Anthropic Claude

Use Anthropic Claude models with Kern agents.

Claude is a family of foundational AI models by Anthropic that can be used in a variety of applications. See their model comparisons here.

We recommend experimenting to find the best-suited model for your use-case. Here are some general recommendations:

  • claude-sonnet-4-20250514 model is good for most use-cases and supports image input.
  • claude-opus-4-1-20250805 model is their best model.
  • claude-3-5-haiku-20241022 model is their fastest model.

Anthropic has rate limits on their APIs. See the docs for more information.

Note

Claude API expects a max_tokens param to be sent with each request. Unless set as a param, Kern will default to 8192. See the docs for more information.

Authentication

Set your ANTHROPIC_API_KEY environment. You can get one from Anthropic here.

1export ANTHROPIC_API_KEY=***
1setx ANTHROPIC_API_KEY ***

Example

Use Claude with your Agent:

1from kern.agent import Agent
2from kern.models.anthropic import Claude
3
4agent = Agent(
5 model=Claude(id="claude-3-5-sonnet-20240620"),
6 markdown=True
7)
8
9# Print the response on the terminal
10agent.print_response("Share a 2 sentence horror story.")

Beta Features

You can use Anthropic's beta features with Kern by setting the betas parameter:

1from kern.agent import Agent
2from kern.models.anthropic import Claude
3
4agent = Agent(
5 model=Claude(
6 betas=["context-management-2025-06-27"],
7 ),
8)

Read more about beta features with Kern Claude model here.

Prompt caching

You can enable system prompt caching by setting cache_system_prompt to True:

1from kern.agent import Agent
2from kern.models.anthropic import Claude
3
4agent = Agent(
5 model=Claude(
6 id="claude-3-5-sonnet-20241022",
7 cache_system_prompt=True,
8 ),
9)

Read more about prompt caching with Kern's Claude model here.

Structured Outputs

Structured outputs are used to ensure that the model's response matches a defined schema.

This is useful to eliminate issues like missing fields or invalid values. Use it for production systems that need reliable, consistent responses in a specific format.

Kern uses Claude's native support for structured outputs. This feature is available for claude-sonnet-4-5-20250929 and all newer models. See Anthropic's structured outputs documentation for more details.

1from kern.agent import Agent
2from kern.models.anthropic import Claude
3from pydantic import BaseModel
4
5class User(BaseModel):
6 name: str
7 age: int
8 email: str
9
10agent = Agent(
11 model=Claude(id="claude-sonnet-4-5-20250929"),
12 description="Extract user information.",
13 output_schema=User,
14)

Read more about structured outputs with Kern's Claude model:

Params

ParameterTypeDefaultDescription
idstr"claude-3-5-sonnet-20241022"The id of the Anthropic Claude model to use
namestr"Claude"The name of the model
providerstr"Anthropic"The provider of the model
max_tokensOptional[int]4096Maximum number of tokens to generate in the chat completion
thinkingOptional[Dict[str, Any]]NoneConfiguration for the thinking (reasoning) process (See their docs))
temperatureOptional[float]NoneControls randomness in the model's output
stop_sequencesOptional[List[str]]NoneA list of strings that the model should stop generating text at
top_pOptional[float]NoneControls diversity via nucleus sampling
top_kOptional[int]NoneControls diversity via top-k sampling
cache_system_promptOptional[bool]FalseWhether to cache the system prompt for improved performance
extended_cache_timeOptional[bool]FalseWhether to use extended cache time (1 hour instead of default)
request_paramsOptional[Dict[str, Any]]NoneAdditional parameters to include in the request
mcp_serversOptional[List[MCPServerConfiguration]]NoneList of MCP (Model Context Protocol) server configurations
api_keyOptional[str]NoneThe API key for authenticating with Anthropic
default_headersOptional[Dict[str, Any]]NoneDefault headers to include in all requests
timeoutOptional[float]NoneRequest timeout in seconds
client_paramsOptional[Dict[str, Any]]NoneAdditional parameters for client configuration
clientOptional[AnthropicClient]NoneA pre-configured instance of the Anthropic client
async_clientOptional[AsyncAnthropicClient]NoneA pre-configured instance of the async Anthropic client

Claude is a subclass of the Model class and has access to the same params.