Skip to main content

Anthropic Provider

The Anthropic provider gives you access to Claude models, known for their strong reasoning capabilities, extended context windows, and thoughtful responses. Claude excels at complex analysis, creative writing, and following detailed instructions.

Installation

pip install agent-framework --pre
pip install agent-framework-anthropic

Authentication

Anthropic supports both direct API access and Azure AI Foundry hosted models.

Direct Anthropic API

import os
from agent_framework.anthropic import AnthropicClient

# Set ANTHROPIC_API_KEY environment variable
# The client will automatically read from the environment
client = AnthropicClient()
.env
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_MODEL_ID=claude-sonnet-4-20250514

Azure AI Foundry (Anthropic via Azure)

from anthropic.foundry import AnthropicFoundryClient
from anthropic.foundry import AnthropicFoundryIdentityTokenCredentials
from azure.identity import DefaultAzureCredential
from agent_framework.anthropic import AnthropicClient

# Using Azure managed identity
credential = DefaultAzureCredential()
foundry_creds = AnthropicFoundryIdentityTokenCredentials(
    credential,
    resource="your-resource-name",
    scopes=["https://ai.azure.com/.default"]
)

client = AnthropicFoundryClient(foundry_creds)
Never commit API keys to source control. Always use environment variables or secure secret management.

Basic Usage

import asyncio
from agent_framework.anthropic import AnthropicClient

async def main():
    # Create agent with Anthropic
    agent = AnthropicClient().as_agent(
        name="Assistant",
        instructions="You are a helpful assistant.",
    )
    
    # Non-streaming response
    result = await agent.run("What is the capital of France?")
    print(result)

asyncio.run(main())

Configuration

# Direct Anthropic API
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_MODEL_ID=claude-sonnet-4-20250514

# Azure AI Foundry
ANTHROPIC_RESOURCE=your-resource-name
ANTHROPIC_CHAT_MODEL_NAME=claude-haiku-4-5

Streaming Responses

import asyncio
from agent_framework.anthropic import AnthropicClient

async def main():
    agent = AnthropicClient().as_agent(
        name="Assistant",
        instructions="You are a helpful assistant.",
    )
    
    # Streaming response
    query = "Write a short story about AI agents."
    print("Agent: ", end="", flush=True)
    async for chunk in agent.run(query, stream=True):
        if chunk.text:
            print(chunk.text, end="", flush=True)
    print()

asyncio.run(main())

Function Calling (Tool Use)

Claude has excellent function calling capabilities:
import asyncio
from typing import Annotated
from agent_framework import tool
from agent_framework.anthropic import AnthropicClient

@tool(approval_mode="never_require")  # Use "always_require" in production
def get_weather(location: Annotated[str, "City name"]) -> str:
    """Get the weather for a location."""
    return f"Weather in {location}: Sunny, 72°F"

@tool(approval_mode="never_require")
def search_web(query: Annotated[str, "Search query"]) -> str:
    """Search the web for information."""
    return f"Search results for: {query}"

async def main():
    agent = AnthropicClient().as_agent(
        name="Assistant",
        instructions="You are a helpful assistant with access to tools.",
        tools=[get_weather, search_web],
    )
    
    result = await agent.run(
        "What's the weather in Seattle and find recent news about it?"
    )
    print(result)

asyncio.run(main())
Claude can intelligently decide when and how to use multiple tools, even calling them in parallel when appropriate.

Available Models

Anthropic offers several Claude model families:
ModelContext WindowBest ForStrengths
claude-sonnet-4-20250514200kBalanced performance and speedGeneral purpose, reasoning
claude-opus-4-20250514200kHighest intelligenceComplex analysis, research
claude-haiku-4-5200kSpeed and efficiencyQuick tasks, cost-effective
claude-3-5-sonnet200kPrevious generationLegacy applications
claude-3-5-haiku200kFast, previous generationQuick responses
All Claude models support function calling, vision (image inputs), and extended thinking (reasoning). Context windows are 200k tokens for all current models.

Extended Thinking (Reasoning)

Claude models support extended thinking for complex reasoning tasks:
import asyncio
from agent_framework.anthropic import AnthropicClient

async def main():
    agent = AnthropicClient(
        model_id="claude-sonnet-4-20250514"
    ).as_agent(
        instructions="You are a reasoning assistant. Think step by step.",
    )
    
    result = await agent.run(
        "Solve this logic puzzle: Three people are in a room. "
        "Alice says Bob is lying. Bob says Charlie is lying. "
        "Charlie says both Alice and Bob are lying. Who is telling the truth?"
    )
    print(result)

asyncio.run(main())
Claude models automatically engage extended thinking for complex problems. You can encourage this behavior by prompting the model to “think step by step” or “analyze carefully.”

Vision Capabilities

All Claude models support image inputs:
import asyncio
from agent_framework import Message
from agent_framework.anthropic import AnthropicClient

async def main():
    agent = AnthropicClient().as_agent(
        instructions="You analyze images in detail.",
    )
    
    message = Message(
        role="user",
        text="What's in this image? Describe it in detail.",
        images=["https://example.com/image.jpg"],
    )
    
    result = await agent.run(message)
    print(result)

asyncio.run(main())
Claude models have excellent vision capabilities and can analyze charts, diagrams, documents, photos, and screenshots with high accuracy.

Long Context

Claude models have 200k token context windows, enabling:
  • Processing entire books or codebases
  • Analyzing lengthy documents
  • Maintaining long conversations
  • Handling extensive research materials
import asyncio
from agent_framework.anthropic import AnthropicClient

async def main():
    agent = AnthropicClient().as_agent(
        instructions="You analyze long documents.",
    )
    
    # Claude can handle very long inputs
    with open("long_document.txt", "r") as f:
        document = f.read()  # Could be 100k+ tokens
    
    result = await agent.run(
        f"Summarize this document and identify key themes:\n\n{document}"
    )
    print(result)

asyncio.run(main())
Claude maintains strong performance even with very long contexts, making it ideal for document analysis and research tasks.

Best Practices

  • Use claude-haiku-4-5 for quick, cost-effective tasks
  • Use claude-sonnet-4 for balanced performance (most use cases)
  • Use claude-opus-4 when maximum intelligence is critical
  • All models support function calling and vision
Claude’s 200k context window is a key advantage:
  1. Process entire documents without chunking
  2. Maintain long conversation history
  3. Analyze multiple related documents together
  4. Keep extensive context for better coherence
Claude responds well to clear, structured prompts:
  1. Be explicit about desired output format
  2. Use XML-style tags for structure (Claude is trained on XML)
  3. Provide examples when possible
  4. Break complex tasks into steps
  5. Use “think step by step” for reasoning tasks
Anthropic has rate limits based on your tier:
  1. Implement exponential backoff retry logic
  2. Monitor usage in the Anthropic console
  3. Consider upgrading your tier for higher limits
  4. Batch similar requests when possible
Claude excels at tool use:
  1. Provide clear, detailed function descriptions
  2. Use descriptive parameter names
  3. Claude can chain multiple tool calls intelligently
  4. Claude can call tools in parallel when appropriate

Troubleshooting

If you see authentication errors:
  1. Verify the API key is correct and active
  2. Check if using Anthropic direct or Azure Foundry
  3. For Azure: ensure managed identity has proper permissions
  4. Verify the resource name is correct for Foundry
If you’re hitting rate limits:
  1. Check your usage tier at console.anthropic.com
  2. Implement exponential backoff
  3. Reduce request frequency
  4. Consider upgrading to a higher tier
  5. Monitor concurrent requests
If the model is not available:
  1. Check if you have access to the model
  2. Verify the model name is correct
  3. Some newer models require API access approval
  4. Check if using the right endpoint (Anthropic vs Azure)
If you exceed context length:
  1. Claude has 200k token limit
  2. Count both input and output tokens
  3. Implement conversation pruning for long sessions
  4. Consider summarizing older context

Cost Optimization

Anthropic pricing varies by model:
  1. Use appropriate models: claude-haiku-4-5 is significantly cheaper than opus-4
  2. Monitor token usage: Input and output tokens are priced differently
  3. Optimize prompts: Shorter, clearer prompts use fewer tokens
  4. Cache system prompts: Reuse system instructions across requests
  5. Leverage long context: Process documents once instead of multiple chunks
Check current pricing at anthropic.com/pricing

Comparison with Other Providers

FeatureClaudeGPT-4oAdvantage
Context Window200k128kClaude
Vision✅ Excellent✅ ExcellentTie
Function Calling✅ Excellent✅ ExcellentTie
Reasoning✅ Extended thinking✅ o1/o3 modelsDifferent approaches
Cost$$$Claude (generally)
Document Analysis✅ Superior✅ GoodClaude
Creative Writing✅ Superior✅ GoodClaude
Code Generation✅ Good✅ SuperiorGPT-4o
Claude excels at document analysis, creative writing, and following complex instructions. GPT-4o may be better for code generation and certain reasoning tasks.

Next Steps

Function Tools

Learn how to add function calling to your agents

Sessions & Memory

Manage multi-turn conversations with memory

Tools

Add function calling to Claude agents

Workflows

Build multi-agent workflows with Claude

Build docs developers (and LLMs) love