Anthropic
Configure Anthropic (Claude) as an LLM provider in agentgateway.
Before you begin
Install and set up an agentgateway proxy.Set up access to Anthropic
-
Get an API key to access the Anthropic API.
-
Save the API key in an environment variable.
export ANTHROPIC_API_KEY=<insert your API key> -
Create a Kubernetes secret to store your Anthropic API key.
kubectl apply -f- <<EOF apiVersion: v1 kind: Secret metadata: name: anthropic-secret namespace: agentgateway-system type: Opaque stringData: Authorization: $ANTHROPIC_API_KEY EOF -
Create an AgentgatewayBackend resource to configure your LLM provider that references the Anthropic API key secret.
kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayBackend metadata: name: anthropic namespace: agentgateway-system spec: ai: provider: anthropic: model: "claude-haiku-4-5-20251001" policies: auth: secretRef: name: anthropic-secret EOFReview the following table to understand this configuration. For more information, see the API reference.
Setting Description ai.provider.anthropicDefine the LLM provider that you want to use. The example uses Anthropic. anthropic.modelThe model to use to generate responses. In this example, you use the claude-haiku-4-5-20251001model.policies.authProvide the credentials to use to access the Anthropic API. The example refers to the secret that you previously created. The token is automatically sent in the x-api-keyheader. -
Create an HTTPRoute resource that routes incoming traffic to the AgentgatewayBackend. The following example sets up a route on the
/anthropicpath. Note that agentgateway automatically rewrites the endpoint to the Anthropic/v1/messagesendpoint.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: anthropic namespace: agentgateway-system spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - backendRefs: - name: anthropic namespace: agentgateway-system group: agentgateway.dev kind: AgentgatewayBackend EOFkubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: anthropic namespace: agentgateway-system spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /v1/chat/completions backendRefs: - name: anthropic namespace: agentgateway-system group: agentgateway.dev kind: AgentgatewayBackend EOFkubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: anthropic namespace: agentgateway-system spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /anthropic backendRefs: - name: anthropic namespace: agentgateway-system group: agentgateway.dev kind: AgentgatewayBackend EOF -
Send a request to the LLM provider API along the route that you previously created. Verify that the request succeeds and that you get back a response from the API.
Cloud Provider LoadBalancer:
curl "$INGRESS_GW_ADDRESS/v1/messages" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqLocalhost:
curl "localhost:8080/v1/messages" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqCloud Provider LoadBalancer:
curl "$INGRESS_GW_ADDRESS/v1/chat/completions" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqLocalhost:
curl "localhost:8080/v1/chat/completions" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqCloud Provider LoadBalancer:
curl "$INGRESS_GW_ADDRESS/anthropic" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqLocalhost:
curl "localhost:8080/anthropic" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqExample output:
{ "model": "claude-haiku-4-5-20251001", "usage": { "prompt_tokens": 16, "completion_tokens": 318, "total_tokens": 334 }, "choices": [ { "message": { "content": "Artificial Intelligence (AI) is a field of computer science that focuses on creating machines that can perform tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation. Here's a simple explanation of how AI works:\n\n1. Data input: AI systems require data to learn and make decisions. This data can be in the form of images, text, numbers, or any other format.\n\n2. Training: The AI system is trained using this data. During training, the system learns to recognize patterns, relationships, and make predictions based on the input data.\n\n3. Algorithms: AI uses various algorithms, which are sets of instructions or rules, to process and analyze the data. These algorithms can be simple or complex, depending on the task at hand.\n\n4. Machine Learning: A subset of AI, machine learning, enables the system to automatically learn and improve from experience without being explicitly programmed. As the AI system is exposed to more data, it can refine its algorithms and become more accurate over time.\n\n5. Output: Once the AI system has processed the data, it generates an output. This output can be a prediction, a decision, or an action, depending on the purpose of the AI system.\n\nAI can be categorized into narrow (weak) AI and general (strong) AI. Narrow AI is designed to perform a specific task, such as playing chess or recognizing speech, while general AI aims to have human-like intelligence that can perform any intellectual task.", "role": "assistant" }, "index": 0, "finish_reason": "stop" } ], "id": "msg_01PbaJfDHnjEBG4BueJNR2ff", "created": 1764627002, "object": "chat.completion" }
Connect to Claude CLI
Configure your AgentgatewayBackend resource to allow connections to the Claude Code CLI.
Keep the following things in mind:
- Model selection: If you specify a specific model in the AgentgatewayBackend resource and then use a different model in the Claude Code CLI, you get a 400 HTTP response with an error message similar to
thinking mode isn't enabled. To use any model, remove thespec.ai.provider.anthropic.modelfield and replace it with{}. - Routes: To use the Claude Code CLI, you must explicitly set the routes that you want to allow. By default, the Claude Code CLI sends requests to the
/v1/messagesAPI endpoint. However, it might send requests to other endpoints, such as/v1/models. To ensure that the Claude Code CLI forwards these requests to Anthropic accordingly without using the/v1/messagesAPI, add a*passthrough route to your AgentgatewayBackend resource as shown in this guide.
-
Update your AgentgatewayBackend resource to allow connections to the Claude Code CLI. The following example sets the default
/v1/messagesand a catch-all passthrough API endpoints, and allows you to use any model via the Claude Code CLI.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayBackend metadata: name: anthropic namespace: agentgateway-system spec: ai: provider: anthropic: {} policies: ai: routes: '/v1/messages': Messages '*': Passthrough auth: secretRef: name: anthropic-secret EOF -
Test the connection via the Claude Code CLI by sending a prompt.
Run the Claude Code CLI with a prompt:
ANTHROPIC_BASE_URL="http://$INGRESS_GW_ADDRESS:80" claude -p "What is a credit card"Start the Claude Code CLI terminal and start prompting it:
ANTHROPIC_BASE_URL="http://$INGRESS_GW_ADDRESS:80" claudeRun the Claude Code CLI with a prompt:
ANTHROPIC_BASE_URL="http://localhost:8080" claude -p "What is a credit card"Start the Claude Code CLI terminal and start prompting it:
ANTHROPIC_BASE_URL="http://localhost:8080" claudeExample output:
A credit card is a payment card issued by a financial institution (typically a bank) that allows the cardholder to borrow funds to pay for goods and services, with the agreement to repay the borrowed amount, usually with interest. ## Key characteristics: **How it works:** - The issuer extends a **credit limit** — the maximum you can spend - You make purchases on credit (borrowed money) - You receive a monthly statement - You can pay the full balance or a minimum payment **Costs:** - **APR (Annual Percentage Rate):** Interest charged on unpaid balances, typically 15-30% - **Annual fee:** Some cards charge a yearly fee - **Late fees:** Charged if you miss payment deadlines **Benefits:** - Build credit history/score - Purchase protections and fraud liability limits - Rewards (cashback, points, miles) - Emergency purchasing power **Key difference from a debit card:** - Debit cards draw directly from your bank account (your money) - Credit cards use borrowed money you repay later **Risks:** - Debt accumulation if balances aren't paid in full - High interest charges - Potential negative impact on credit score if mismanaged In short: a credit card is a short-term loan instrument that, when used responsibly, offers convenience and benefits, but can become costly if balances carry over month-to-month.
Next steps
- Want to use other endpoints than chat completions, such as embeddings or models? Check out the multiple endpoints guide.
- Explore other guides for LLM consumption, such as function calling, model failover, and prompt guards.