|
| 1 | +--- |
| 2 | +title: Anthropic Agents |
| 3 | +description: Learn how to use the Microsoft Agent Framework with Anthropic's Claude models. |
| 4 | +zone_pivot_groups: programming-languages |
| 5 | +author: eavanvalkenburg |
| 6 | +ms.topic: tutorial |
| 7 | +ms.author: edvan |
| 8 | +ms.date: 11/05/2025 |
| 9 | +ms.service: agent-framework |
| 10 | +--- |
| 11 | + |
| 12 | +# Anthropic Agents |
| 13 | + |
| 14 | +The Microsoft Agent Framework supports creating agents that use [Anthropic's Claude models](https://www.anthropic.com/claude). |
| 15 | + |
| 16 | +::: zone pivot="programming-language-csharp" |
| 17 | + |
| 18 | +Coming soon... |
| 19 | + |
| 20 | +::: zone-end |
| 21 | +::: zone pivot="programming-language-python" |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +Install the Microsoft Agent Framework Anthropic package. |
| 26 | + |
| 27 | +```bash |
| 28 | +pip install agent-framework-anthropic --pre |
| 29 | +``` |
| 30 | + |
| 31 | +## Configuration |
| 32 | + |
| 33 | +### Environment Variables |
| 34 | + |
| 35 | +Set up the required environment variables for Anthropic authentication: |
| 36 | + |
| 37 | +```bash |
| 38 | +# Required for Anthropic API access |
| 39 | +ANTHROPIC_API_KEY="your-anthropic-api-key" |
| 40 | +ANTHROPIC_CHAT_MODEL_ID="claude-sonnet-4-5-20250929" # or your preferred model |
| 41 | +``` |
| 42 | + |
| 43 | +Alternatively, you can use a `.env` file in your project root: |
| 44 | + |
| 45 | +```env |
| 46 | +ANTHROPIC_API_KEY=your-anthropic-api-key |
| 47 | +ANTHROPIC_CHAT_MODEL_ID=claude-sonnet-4-5-20250929 |
| 48 | +``` |
| 49 | + |
| 50 | +You can get an API key from the [Anthropic Console](https://console.anthropic.com/). |
| 51 | + |
| 52 | +## Getting Started |
| 53 | + |
| 54 | +Import the required classes from the Agent Framework: |
| 55 | + |
| 56 | +```python |
| 57 | +import asyncio |
| 58 | +from agent_framework.anthropic import AnthropicClient |
| 59 | +``` |
| 60 | + |
| 61 | +## Creating an Anthropic Agent |
| 62 | + |
| 63 | +### Basic Agent Creation |
| 64 | + |
| 65 | +The simplest way to create an Anthropic agent: |
| 66 | + |
| 67 | +```python |
| 68 | +async def basic_example(): |
| 69 | + # Create an agent using Anthropic |
| 70 | + agent = AnthropicClient().create_agent( |
| 71 | + name="HelpfulAssistant", |
| 72 | + instructions="You are a helpful assistant.", |
| 73 | + ) |
| 74 | + |
| 75 | + result = await agent.run("Hello, how can you help me?") |
| 76 | + print(result.text) |
| 77 | +``` |
| 78 | + |
| 79 | +### Using Explicit Configuration |
| 80 | + |
| 81 | +You can provide explicit configuration instead of relying on environment variables: |
| 82 | + |
| 83 | +```python |
| 84 | +async def explicit_config_example(): |
| 85 | + agent = AnthropicClient( |
| 86 | + model_id="claude-sonnet-4-5-20250929", |
| 87 | + api_key="your-api-key-here", |
| 88 | + ).create_agent( |
| 89 | + name="HelpfulAssistant", |
| 90 | + instructions="You are a helpful assistant.", |
| 91 | + ) |
| 92 | + |
| 93 | + result = await agent.run("What can you do?") |
| 94 | + print(result.text) |
| 95 | +``` |
| 96 | + |
| 97 | +## Agent Features |
| 98 | + |
| 99 | +### Function Tools |
| 100 | + |
| 101 | +Equip your agent with custom functions: |
| 102 | + |
| 103 | +```python |
| 104 | +from typing import Annotated |
| 105 | + |
| 106 | +def get_weather( |
| 107 | + location: Annotated[str, "The location to get the weather for."], |
| 108 | +) -> str: |
| 109 | + """Get the weather for a given location.""" |
| 110 | + conditions = ["sunny", "cloudy", "rainy", "stormy"] |
| 111 | + return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C." |
| 112 | + |
| 113 | +async def tools_example(): |
| 114 | + agent = AnthropicClient().create_agent( |
| 115 | + name="WeatherAgent", |
| 116 | + instructions="You are a helpful weather assistant.", |
| 117 | + tools=get_weather, # Add tools to the agent |
| 118 | + ) |
| 119 | + |
| 120 | + result = await agent.run("What's the weather like in Seattle?") |
| 121 | + print(result.text) |
| 122 | +``` |
| 123 | + |
| 124 | +### Streaming Responses |
| 125 | + |
| 126 | +Get responses as they are generated for better user experience: |
| 127 | + |
| 128 | +```python |
| 129 | +async def streaming_example(): |
| 130 | + agent = AnthropicClient().create_agent( |
| 131 | + name="WeatherAgent", |
| 132 | + instructions="You are a helpful weather agent.", |
| 133 | + tools=get_weather, |
| 134 | + ) |
| 135 | + |
| 136 | + query = "What's the weather like in Portland and in Paris?" |
| 137 | + print(f"User: {query}") |
| 138 | + print("Agent: ", end="", flush=True) |
| 139 | + async for chunk in agent.run_stream(query): |
| 140 | + if chunk.text: |
| 141 | + print(chunk.text, end="", flush=True) |
| 142 | + print() |
| 143 | +``` |
| 144 | + |
| 145 | +### Hosted Tools |
| 146 | + |
| 147 | +Anthropic agents support hosted tools such as web search, MCP (Model Context Protocol), and code execution: |
| 148 | + |
| 149 | +```python |
| 150 | +from agent_framework import HostedMCPTool, HostedWebSearchTool |
| 151 | + |
| 152 | +async def hosted_tools_example(): |
| 153 | + agent = AnthropicClient().create_agent( |
| 154 | + name="DocsAgent", |
| 155 | + instructions="You are a helpful agent for both Microsoft docs questions and general questions.", |
| 156 | + tools=[ |
| 157 | + HostedMCPTool( |
| 158 | + name="Microsoft Learn MCP", |
| 159 | + url="https://learn.microsoft.com/api/mcp", |
| 160 | + ), |
| 161 | + HostedWebSearchTool(), |
| 162 | + ], |
| 163 | + max_tokens=20000, |
| 164 | + ) |
| 165 | + |
| 166 | + result = await agent.run("Can you compare Python decorators with C# attributes?") |
| 167 | + print(result.text) |
| 168 | +``` |
| 169 | + |
| 170 | +### Extended Thinking (Reasoning) |
| 171 | + |
| 172 | +Anthropic supports extended thinking capabilities through the `thinking` feature, which allows the model to show its reasoning process: |
| 173 | + |
| 174 | +```python |
| 175 | +from agent_framework import TextReasoningContent, UsageContent |
| 176 | + |
| 177 | +async def thinking_example(): |
| 178 | + agent = AnthropicClient().create_agent( |
| 179 | + name="DocsAgent", |
| 180 | + instructions="You are a helpful agent.", |
| 181 | + tools=[HostedWebSearchTool()], |
| 182 | + max_tokens=20000, |
| 183 | + additional_chat_options={ |
| 184 | + "thinking": {"type": "enabled", "budget_tokens": 10000} |
| 185 | + }, |
| 186 | + ) |
| 187 | + |
| 188 | + query = "Can you compare Python decorators with C# attributes?" |
| 189 | + print(f"User: {query}") |
| 190 | + print("Agent: ", end="", flush=True) |
| 191 | + |
| 192 | + async for chunk in agent.run_stream(query): |
| 193 | + for content in chunk.contents: |
| 194 | + if isinstance(content, TextReasoningContent): |
| 195 | + # Display thinking in a different color |
| 196 | + print(f"\033[32m{content.text}\033[0m", end="", flush=True) |
| 197 | + if isinstance(content, UsageContent): |
| 198 | + print(f"\n\033[34m[Usage: {content.details}]\033[0m\n", end="", flush=True) |
| 199 | + if chunk.text: |
| 200 | + print(chunk.text, end="", flush=True) |
| 201 | + print() |
| 202 | +``` |
| 203 | + |
| 204 | +## Using the Agent |
| 205 | + |
| 206 | +The agent is a standard `BaseAgent` and supports all standard agent operations. |
| 207 | + |
| 208 | +See the [Agent getting started tutorials](../../../tutorials/overview.md) for more information on how to run and interact with agents. |
| 209 | + |
| 210 | +::: zone-end |
| 211 | + |
| 212 | +## Next steps |
| 213 | + |
| 214 | +> [!div class="nextstepaction"] |
| 215 | +> [Azure AI Agents](./azure-ai-foundry-agent.md) |
0 commit comments