Anthropic
Configure Anthropic (Claude) as an LLM provider in agentgateway.
Before you begin
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: kgateway-system type: Opaque stringData: Authorization: $ANTHROPIC_API_KEY EOF -
Create a Backend resource to configure an LLM provider that references the Anthropic API key secret.
kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: Backend metadata: name: anthropic namespace: kgateway-system spec: type: AI ai: llm: anthropic: authToken: kind: SecretRef secretRef: name: anthropic-secret model: "claude-3-opus-20240229" apiVersion: "2023-06-01" EOFReview the following table to understand this configuration. For more information, see the API reference.
Setting Description typeSet to AIto configure this Backend for an AI provider.aiDefine the AI backend configuration. The example uses Anthropic ( spec.ai.llm.anthropic).authTokenConfigure the authentication token for Anthropic API. The example refers to the secret that you previously created. The token is automatically sent in the x-api-keyheader.modelOptional: Override the model name, such as claude-3-opus-20240229. If unset, the model name is taken from the request.apiVersionOptional: A version header to pass to the Anthropic API. For more information, see the Anthropic API versioning docs. -
Create an HTTPRoute resource that routes incoming traffic to the Backend. The following example sets up a route on the
/anthropicpath. Note that kgateway 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: kgateway-system spec: parentRefs: - name: agentgateway namespace: kgateway-system rules: - matches: - path: type: PathPrefix value: /anthropic backendRefs: - name: anthropic namespace: kgateway-system group: gateway.kgateway.dev kind: Backend EOF -
Send a request to the LLM provider API. Note that Anthropic uses the
/v1/messagesendpoint format instead of/v1/chat/completions. Verify that the request succeeds and that you get back a response from the API.curl "$INGRESS_GW_ADDRESS:8080/anthropic" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqcurl "localhost:8080/anthropic" -H content-type:application/json -d '{ "model": "", "messages": [ { "role": "user", "content": "Explain how AI works in simple terms." } ] }' | jqExample output:
{ "id": "msg_0123456789abcdef", "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "AI, or artificial intelligence, works by training computers to recognize patterns in data and make predictions or decisions based on those patterns. Think of it like teaching a computer to recognize cats in photos by showing it thousands of cat pictures - eventually it learns what makes a cat a cat. AI systems use algorithms and mathematical models to process information, learn from examples, and then apply that knowledge to new situations." } ], "model": "claude-3-opus-20240229", "stop_reason": "end_turn", "stop_sequence": null, "usage": { "input_tokens": 10, "output_tokens": 85 } }