feat: add ANTHROPIC_BASE_URL support#1886
Conversation
There was a problem hiding this comment.
Pull request overview
Adds first-class support for overriding Anthropic API base URLs via ANTHROPIC_BASE_URL across the Go controller/runtime env registration and the Python ADK Anthropic model wrapper, plus introduces a new basic-anthropic sample and an E2E test that invokes it against a mock Anthropic server.
Changes:
- Register and inject
ANTHROPIC_BASE_URLinto agent pods when an Anthropic model is configured with a custom BaseURL (Go controller). - Teach the Python ADK Anthropic model wrapper to read
ANTHROPIC_BASE_URLwhen no explicitbase_urlis set. - Add a
basic-anthropicsample (Dockerfile, YAML, agent-card) and a Go E2E test + mock fixture for Anthropic invocation.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| python/samples/anthropic/basic_agent/README.md | Adds a short README for the new Anthropic sample. |
| python/samples/anthropic/basic_agent/pyproject.toml | Defines the sample Python package and dependencies. |
| python/samples/anthropic/basic_agent/Dockerfile | Builds a container image for the Anthropic sample agent. |
| python/samples/anthropic/basic_agent/basic_agent/agent.py | Implements the Anthropic ADK agent + tools and exposes it via KAgent ADK app. |
| python/samples/anthropic/basic_agent/basic_agent/agent-card.json | Adds an A2A agent card artifact for the sample. |
| python/samples/anthropic/basic_agent/basic_agent/init.py | Exposes the sample app object. |
| python/samples/anthropic/basic_agent/agent.yaml | Adds a KAgent Agent CR example for deploying the sample. |
| python/pyproject.toml | Adds Anthropic samples to the uv workspace members list. |
| python/packages/kagent-adk/src/kagent/adk/models/_anthropic.py | Reads ANTHROPIC_BASE_URL from env when creating the Anthropic SDK client. |
| python/packages/kagent-adk/src/kagent/adk/_a2a.py | Logs presence of ANTHROPIC_BASE_URL at app build time. |
| python/Makefile | Adds basic-anthropic-sample docker build target. |
| Makefile | Adds basic-anthropic to the push-test-agent build/push list. |
| go/core/test/e2e/mocks/invoke_anthropic_agent.json | Adds mock Anthropic responses for calculator/weather tool flow. |
| go/core/test/e2e/invoke_api_test.go | Adds E2E test that deploys and invokes the Anthropic BYO agent image against the mock server. |
| go/core/pkg/env/providers.go | Registers ANTHROPIC_BASE_URL env var for agent runtime injection. |
| go/core/internal/controller/translator/agent/adk_api_translator.go | Injects ANTHROPIC_BASE_URL into pods when Anthropic BaseURL is configured. |
Comments suppressed due to low confidence (2)
python/samples/anthropic/basic_agent/basic_agent/agent.py:101
kagent.adk.KAgentAppstoresagent_cardwithout validation and passes it through toA2AFastAPIApplication; in this package the expected type isa2a.types.AgentCard(seekagent/adk/_a2a.py). Passing a plain dict here is likely to fail when the A2A app tries to use AgentCard fields/methods; construct anAgentCard(or callAgentCard.model_validate(...)) before passing it in, similar topython/packages/kagent-openai/src/kagent/openai/_a2a.py.
app = KAgentApp(
root_agent_factory=lambda: root_agent,
agent_card={
"name": "basic-anthropic-agent",
"description": "A basic Anthropic agent with calculator and weather tools",
"url": "localhost:8000",
"version": "0.1.0",
"capabilities": {"streaming": True},
"defaultInputModes": ["text"],
"defaultOutputModes": ["text"],
"skills": [
{
"id": "basic",
"name": "Basic Assistant",
"description": "Can perform calculations and get weather information",
"tags": ["calculator", "weather", "assistant"],
}
],
},
python/samples/anthropic/basic_agent/basic_agent/agent.py:90
- The agent card URL/port is inconsistent across the sample: here it's set to
localhost:8000, whilebasic_agent/agent-card.jsonuseshttp://localhost:8080and the container exposes 8080. This inconsistency can break discovery/clients depending on which artifact is used; please standardize the URL (including scheme + port) across the sample.
"name": "basic-anthropic-agent",
"description": "A basic Anthropic agent with calculator and weather tools",
"url": "localhost:8000",
"version": "0.1.0",
"capabilities": {"streaming": True},
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
EItanya
left a comment
There was a problem hiding this comment.
Thanks so much for the contribution. Can we leave the new sample agent out of this PR. Frankly the existing sample agents are already causing issues with dependencies and other issues. I think a separate repo with sample agents might be a better long-term solution, but for now can you please remove it.
Thanks @EItanya |
ANTHROPIC_BASE_URL support and basic-anthropic agent sample ANTHROPIC_BASE_URL support
|
cc @EItanya Please re-check it |
Signed-off-by: dongjiang <dongjiang1989@126.com>
| kwargs["api_key"] = api_key | ||
| if self.base_url: | ||
| kwargs["base_url"] = self.base_url | ||
| if base_url: |
There was a problem hiding this comment.
What would be a use case to inject the ANTHROPIC_BASE_URL env var? As shown here we already set the base_url kwarg on the anthropic client (which has a higher order of precedence than the env var), so if you specify a base url in the modelconfig for anthropic models it already works.
There was a problem hiding this comment.
Thanks @supreme-gg-gg
Totally agree. The explicit base_url passed into the Anthropic client has higher precedence and fully covers all custom endpoint requirements defined in ModelConfig.
We keep supporting the ANTHROPIC_BASE_URL environment variable mainly for native SDK convention compliance, convenient local debugging, unified global proxy configuration, and compatibility with external tooling that only respects official Anthropic environment variables.
Additionally, it provides a smooth out-of-the-box experience for new users: when first installing kagent, they can simply set ANTHROPIC_BASE_URL(with a proxy URL) and ANTHROPIC_API_KEY at the system level to get a working default setup immediately, without needing to configure ModelConfig upfront.
Summary
ANTHROPIC_BASE_URLenvironment variable support across Go controller and Python ADK, mirroring the existingOPENAI_API_BASEpatternANTHROPIC_BASE_URLinto agent pods when Anthropic provider has a custom BaseURL configured