Claude Code for KServe: Kubernetes-Native Model Serving — Claude Skills 360 Blog
Blog / AI / Claude Code for KServe: Kubernetes-Native Model Serving
AI

Claude Code for KServe: Kubernetes-Native Model Serving

Published: September 1, 2027
Read time: 5 min read
By: Claude Skills 360

KServe is the Kubernetes-native model serving platform, formerly KFServing. kubectl apply -f inference-service.yaml deploys a model. InferenceService CRD declares the serving spec: apiVersion: serving.kserve.io/v1beta1, kind: InferenceService, spec.predictor defines the model server — sklearn, tensorflow, pytorch, onnx, xgboost, huggingface, or custom. Storage URIs: storageUri: s3://bucket/model/ or gs:// or pvc://. Serverless: Knative autoscaling scales to zero when idle — autoscaling.knative.dev/target: "10" sets concurrent requests target. Raw Kubernetes mode: serving.kserve.io/deploymentMode: RawDeployment skips Knative for always-on deployments. Canary rollout: spec.predictor.canaryTrafficPercent: 20 with a newModel spec — splits 20% to new model version. Transformer: spec.transformer adds a custom pre/postprocessing container that proxies requests to the predictor. Explainer: spec.explainer.alibi: { type: AnchorTabular } adds SHAP/Anchor explanations via the /explain endpoint. kubectl get inferenceservice shows status, URL, and traffic. Inference: POST https://{service}.{namespace}.{domain}/v1/models/{name}:predict with { "instances": [...] }. V2 API: POST /v2/models/{name}/infer with { "inputs": [{ "name": "input-0", "shape": [...], "datatype": "FP32", "data": [...] }] }. Python SDK: from kserve import KServeClient, KServeClient().create(isvc). ModelMesh for multi-model serving co-locates many models on fewer pods. Claude Code generates InferenceService YAMLs, transformer handlers, canary configs, Python SDK scripts, and TypeScript inference clients.

CLAUDE.md for KServe

## KServe Stack
- Version: kserve >= 0.12 (KFServing successor)
- CRD: InferenceService (serving.kserve.io/v1beta1) — spec.predictor + optional transformer/explainer
- Predictors: sklearn, tensorflow, pytorch, onnx, xgboost, huggingface, triton, custom
- Storage: storageUri: s3:// gs:// pvc:// (cluster StorageClass) + ServiceAccountName
- API: POST /v1/models/{name}:predict {"instances": [...]} or V2 /v2/models/{name}/infer
- Canary: spec.predictor.canaryTrafficPercent + newModel for progressive rollout
- Python SDK: from kserve import KServeClient, V1beta1InferenceService

InferenceService YAML Specs

# k8s/sklearn-isvc.yaml — scikit-learn model on S3
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
  name: churn-classifier
  namespace: ml-serving
  annotations:
    serving.kserve.io/deploymentMode: Serverless
    autoscaling.knative.dev/target: "20"
    autoscaling.knative.dev/minScale: "0"
    autoscaling.knative.dev/maxScale: "5"
spec:
  predictor:
    serviceAccountName: kserve-s3-sa
    sklearn:
      storageUri: s3://my-models/churn/v1/
      resources:
        requests:
          cpu: "500m"
          memory: "512Mi"
        limits:
          cpu: "2"
          memory: "2Gi"
---
# k8s/huggingface-isvc.yaml — HuggingFace transformer model (GPU)
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
  name: sentiment-service
  namespace: ml-serving
  annotations:
    serving.kserve.io/deploymentMode: RawDeployment   # Always-on, no Knative
spec:
  predictor:
    serviceAccountName: kserve-s3-sa
    minReplicas: 1
    maxReplicas: 4
    scaleTarget: 10
    huggingface:
      storageUri:  s3://my-models/sentiment/v2/
      protocolVersion: v2
      args:
        - "--model_name=sentiment"
        - "--model_id=cardiffnlp/twitter-roberta-base-sentiment-latest"
      resources:
        requests:
          cpu: "2"
          memory: "8Gi"
          nvidia.com/gpu: "1"
        limits:
          cpu: "4"
          memory: "16Gi"
          nvidia.com/gpu: "1"
---
# k8s/canary-isvc.yaml — canary rollout 20% to v2
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
  name: recommender
  namespace: ml-serving
spec:
  predictor:
    canaryTrafficPercent: 20
    # Current stable model (80% traffic)
    pytorch:
      storageUri:     s3://my-models/recommender/v1/
      protocolVersion: v2
    # Canary model (20% traffic)
    newModel:
      pytorch:
        storageUri:     s3://my-models/recommender/v2/
        protocolVersion: v2

Custom Transformer

# transformer/transformer.py — KServe Transformer for pre/postprocessing
from __future__ import annotations
import json
import logging
from typing import Any

import kserve
import numpy as np

logger = logging.getLogger(__name__)


class FeatureTransformer(kserve.Model):
    """
    KServe Transformer: preprocesses raw API requests before sending
    to the predictor, and formats raw predictions for the client.
    """

    def __init__(self, name: str, predictor_host: str):
        super().__init__(name)
        self.predictor_host = predictor_host
        self.ready          = False
        self.feature_names: list[str] = [
            "age", "tenure_days", "monthly_spend",
            "support_tickets", "last_login_days",
        ]

    def load(self) -> bool:
        # Load any preprocessing artifacts (scalers, encoders)
        # In production these would come from a model store
        from sklearn.preprocessing import StandardScaler
        import joblib
        try:
            self.scaler = joblib.load("/mnt/models/scaler.pkl")
        except FileNotFoundError:
            self.scaler = None
            logger.warning("No scaler found — using raw features")
        self.ready = True
        return self.ready

    def preprocess(self, payload: dict, headers: dict[str, str] | None = None) -> dict:
        """Transform raw request into model input format."""
        instances = payload.get("instances", [])
        processed = []

        for instance in instances:
            if isinstance(instance, dict):
                features = [float(instance.get(f, 0.0)) for f in self.feature_names]
            else:
                features = [float(v) for v in instance]

            if self.scaler:
                features = self.scaler.transform([features])[0].tolist()
            processed.append(features)

        return {"instances": processed}

    def postprocess(self, response: dict, headers: dict[str, str] | None = None) -> dict:
        """Format raw model output for API consumers."""
        predictions = response.get("predictions", [])
        results = []

        for pred in predictions:
            if isinstance(pred, list):
                churn_prob = float(pred[1]) if len(pred) > 1 else float(pred[0])
            else:
                churn_prob = float(pred)

            results.append({
                "churn_probability": round(churn_prob, 4),
                "risk_tier": "HIGH" if churn_prob > 0.7 else "MEDIUM" if churn_prob > 0.3 else "LOW",
            })

        return {"predictions": results}


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--model_name",     type=str, required=True)
    parser.add_argument("--predictor_host", type=str, required=True)
    args = parser.parse_args()

    model = FeatureTransformer(args.model_name, args.predictor_host)
    model.load()
    kserve.ModelServer().start([model])

Python SDK

# scripts/manage_isvc.py — KServe Python SDK for InferenceService management
from __future__ import annotations
from kserve import (
    KServeClient,
    V1beta1InferenceService,
    V1beta1InferenceServiceSpec,
    V1beta1PredictorSpec,
    V1beta1SKLearnSpec,
    V1beta1HuggingFaceRuntimeSpec,
    constants,
)
from kubernetes import client


def create_sklearn_service(
    name:       str,
    namespace:  str,
    storage_uri: str,
    sa_name:    str = "kserve-s3-sa",
) -> V1beta1InferenceService:
    """Create an sklearn InferenceService."""
    isvc = V1beta1InferenceService(
        api_version=constants.KSERVE_V1BETA1,
        kind=constants.KSERVE_KIND,
        metadata=client.V1ObjectMeta(
            name=name,
            namespace=namespace,
            annotations={
                "serving.kserve.io/deploymentMode": "Serverless",
            },
        ),
        spec=V1beta1InferenceServiceSpec(
            predictor=V1beta1PredictorSpec(
                service_account_name=sa_name,
                sklearn=V1beta1SKLearnSpec(
                    storage_uri=storage_uri,
                    resources=client.V1ResourceRequirements(
                        requests={"cpu": "500m", "memory": "512Mi"},
                        limits={"cpu": "2", "memory": "2Gi"},
                    ),
                ),
            )
        ),
    )

    kserve_client = KServeClient()
    kserve_client.create(isvc, namespace=namespace)
    print(f"Created InferenceService: {name} in {namespace}")
    return isvc


def wait_for_ready(name: str, namespace: str, timeout: int = 300) -> str:
    """Poll until InferenceService is ready and return its URL."""
    kserve_client = KServeClient()
    kserve_client.wait_isvc_ready(name, namespace=namespace, timeout_seconds=timeout)
    isvc = kserve_client.get(name, namespace=namespace)
    url  = isvc.status.url
    print(f"Ready: {url}")
    return url


def rollout_canary(
    name:         str,
    namespace:    str,
    new_storage:  str,
    canary_pct:   int = 20,
) -> None:
    """Patch InferenceService to add canary traffic split."""
    kserve_client = KServeClient()
    patch = {
        "spec": {
            "predictor": {
                "canaryTrafficPercent": canary_pct,
                "newModel": {
                    "sklearn": {"storageUri": new_storage}
                },
            }
        }
    }
    kserve_client.patch(name, patch, namespace=namespace)
    print(f"Canary rollout: {canary_pct}% to new model for {name}")


if __name__ == "__main__":
    create_sklearn_service(
        name="churn-classifier",
        namespace="ml-serving",
        storage_uri="s3://my-models/churn/v1/",
    )
    wait_for_ready("churn-classifier", "ml-serving")

TypeScript Client

// lib/kserve/client.ts — TypeScript client for KServe V1/V2 inference
const KSERVE_URL = process.env.KSERVE_URL ?? "http://churn-classifier.ml-serving.svc.cluster.local"

// V1 inference protocol
export async function predictV1<T>(
  modelName: string,
  instances: unknown[],
): Promise<{ predictions: T[] }> {
  const res = await fetch(`${KSERVE_URL}/v1/models/${modelName}:predict`, {
    method:  "POST",
    headers: { "Content-Type": "application/json" },
    body:    JSON.stringify({ instances }),
  })
  if (!res.ok) throw new Error(`KServe ${res.status}: ${await res.text()}`)
  return res.json()
}

// V2 inference protocol (Open Inference Protocol)
export async function predictV2(
  modelName: string,
  inputs: Array<{
    name:     string
    shape:    number[]
    datatype: "FP32" | "FP64" | "INT32" | "INT64" | "BYTES"
    data:     number[] | string[]
  }>,
): Promise<{ outputs: Array<{ name: string; shape: number[]; datatype: string; data: unknown[] }> }> {
  const res = await fetch(`${KSERVE_URL}/v2/models/${modelName}/infer`, {
    method:  "POST",
    headers: { "Content-Type": "application/json" },
    body:    JSON.stringify({ inputs }),
  })
  if (!res.ok) throw new Error(`KServe V2 ${res.status}: ${await res.text()}`)
  return res.json()
}

export async function getModelMetadata(modelName: string) {
  const res = await fetch(`${KSERVE_URL}/v2/models/${modelName}`)
  return res.json()
}

For the Seldon Core alternative when needing a full MLOps platform with built-in A/B testing orchestration, multi-armed bandit routing, outlier detection, model explainability, and a Seldon Deploy UI — Seldon Core offers a richer operator ecosystem for enterprise ML governance while KServe is the CNCF-preferred standard with lighter footprint and tighter Knative serverless integration for autoscaling to zero. For the BentoML alternative when packaging models into self-contained Docker images without Kubernetes orchestration — BentoML’s bentofile.yaml approach is simpler for teams without Kubernetes expertise while KServe is the right choice for teams already running workloads on Kubernetes who want declarative kubectl apply model deployments with built-in canary rollouts and scale-to-zero. The Claude Skills 360 bundle includes KServe skill sets covering InferenceService YAMLs, transformer handlers, Python SDK management, and canary rollouts. Start with the free tier to try Kubernetes model serving generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free