Anthropic plugin
The genkit-anthropic package integrates Anthropic’s Claude models via the Messages API.
Installation
Section titled “Installation”uv add genkit-anthropicConfiguration
Section titled “Configuration”Register Anthropic on Genkit and reference models with Anthropic.claude() (or 'anthropic/<model-id>' strings):
from genkit import Genkitfrom genkit_anthropic import Anthropic
ai = Genkit( plugins=[Anthropic()], model=Anthropic.claude('claude-sonnet-4-6'),)Authentication and client options
Section titled “Authentication and client options”Set ANTHROPIC_API_KEY in the environment, pass api_key= at plugin initialization, or override per request via context={'secrets': {'api_key': ...}}:
import osfrom genkit import Genkitfrom genkit_anthropic import Anthropic
ai = Genkit( plugins=[ Anthropic( api_key=os.environ['ANTHROPIC_API_KEY'], # base_url takes the origin only; a value ending in '/v1' raises: # base_url='https://anthropic-proxy.internal', client_options={'timeout': 60.0, 'max_retries': 3}, ) ], model=Anthropic.claude('claude-sonnet-4-6'),)
# Per-request key override. Traces record config but not secrets, so the# plugin rejects a key in config with INVALID_ARGUMENT.# tenant_api_key comes from your own per-tenant secret store.response = await ai.generate( model=Anthropic.claude('claude-haiku-4-5'), prompt='Check this invoice total.', context={'secrets': {'api_key': tenant_api_key}},)Available models
Section titled “Available models”claude-haiku-4-5— Lowest latency and costclaude-sonnet-4-6— Balanced coding, tool use, and extended thinkingclaude-opus-4-8— Highest capability for complex reasoning and agentic loopsclaude-opus-4-7— Previous-generation Opus model
Any other Claude ID resolves on demand, so Anthropic.claude('<model-id>') works for models released after this plugin version.
Basic usage
Section titled “Basic usage”response = await ai.generate( prompt='Explain write-ahead logging in three sentences.',)print(response.text)Structured output
Section titled “Structured output”from pydantic import BaseModel, Field
class IncidentSummary(BaseModel): service: str = Field(description='Affected service name') severity: str = Field(description='P0, P1, or P2') root_cause: str = Field(description='One-line root cause')
response = await ai.generate( prompt='Summarize: checkout-api returned 503s for 4 minutes after a connection pool leak.', output_schema=IncidentSummary,)summary = response.outputif summary is None: raise RuntimeError(f'Summary generation failed: {response.error}')print(summary.service, summary.severity)# => checkout-api P1Thinking and reasoning
Section titled “Thinking and reasoning”Configure extended or adaptive thinking with flat fields on AnthropicConfig:
from genkit_anthropic import Anthropic, AnthropicConfig
# Manual token budgetresponse = await ai.generate( model=Anthropic.claude('claude-sonnet-4-6'), prompt='Prove that the square root of 2 is irrational.', config=AnthropicConfig( thinking='enabled', thinking_budget=1024, max_output_tokens=4096, ),)reasoning = ''.join(p.reasoning or '' for p in response.message.content)print(reasoning)print(response.text)
# Adaptive thinking: the model sizes its own budget.# task_budget (minimum 20,000) sends the request on the beta API.adaptive = await ai.generate( model=Anthropic.claude('claude-opus-4-8'), prompt='Audit this distributed lock protocol for split-brain edge cases.', config=AnthropicConfig( thinking='adaptive', effort='high', # 'low', 'medium', 'high', 'xhigh', or 'max' task_budget=20_000, ),)print(adaptive.text)Tool calling
Section titled “Tool calling”Pass tool_choice ('auto', 'required', or 'none') on ai.generate(). To restrict Claude to at most one tool call per turn, set disable_parallel_tool_use=True on AnthropicConfig:
from pydantic import BaseModel, Fieldfrom genkit_anthropic import AnthropicConfig
class WeatherInput(BaseModel): location: str = Field(description='City name')
@ai.tool()async def get_weather(input: WeatherInput) -> str: """Get current weather for a location.""" return f'72°F and clear in {input.location}'
response = await ai.generate( prompt='What is the weather in San Francisco?', tools=[get_weather], tool_choice='auto', config=AnthropicConfig(disable_parallel_tool_use=True),)print(response.text)Streaming
Section titled “Streaming”from genkit import FlowContext
@ai.flow()async def streaming_analysis(topic: str, ctx: FlowContext[str]) -> str: stream_response = ai.generate_stream( prompt=f'Outline failure modes for {topic}.', ) async for chunk in stream_response.stream: ctx.send_chunk(chunk.text) return (await stream_response.response).textMultimodal (image and PDF input)
Section titled “Multimodal (image and PDF input)”Build multimodal prompts with Part.from_text() and Part.from_media(). The plugin sends image/* media as image blocks and application/pdf as document blocks, from a URL or a data: URI:
from genkit import Part
response = await ai.generate( prompt=[ Part.from_text('Extract the table headers from this image.'), Part.from_media( url='https://example.com/report-page.jpg', content_type='image/jpeg', ), ],)print(response.text)
contract = await ai.generate( prompt=[ Part.from_text('List the termination clauses in this contract.'), Part.from_media( url='https://example.com/vendor-contract.pdf', content_type='application/pdf', ), ],)print(contract.text)Configuration options
Section titled “Configuration options”AnthropicConfig rejects unknown top-level keys. api_version='beta' selects the beta API, and betas replaces the plugin’s default beta headers. Undeclared request fields go in extra, which is merged into the top level of the request body:
from genkit_anthropic import AnthropicConfig
response = await ai.generate( prompt='Draft a migration checklist.', config=AnthropicConfig( temperature=0.4, max_output_tokens=1024, user_id='acct_9481', api_version='beta', betas=['files-api-2025-04-14'], extra={'container': 'cntr_01'}, ),)