|
| 1 | +# Builtin Tools |
| 2 | + |
| 3 | +Builtin tools are native tools provided by LLM providers that can be used to enhance your agent's capabilities. Unlike [common tools](common-tools.md), which are custom implementations that PydanticAI executes, builtin tools are executed directly by the model provider. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +PydanticAI supports two types of builtin tools: |
| 8 | + |
| 9 | +- **[`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool]**: Allows agents to search the web |
| 10 | +- **[`CodeExecutionTool`][pydantic_ai.builtin_tools.CodeExecutionTool]**: Enables agents to execute code in a secure environment |
| 11 | + |
| 12 | +These tools are passed to the agent via the `builtin_tools` parameter and are executed by the model provider's infrastructure. |
| 13 | + |
| 14 | +!!! warning "Provider Support" |
| 15 | + Not all model providers support builtin tools. If you use a builtin tool with an unsupported provider, PydanticAI will raise a [`UserError`][pydantic_ai.exceptions.UserError] when you try to run the agent. |
| 16 | + |
| 17 | +## Web Search Tool |
| 18 | + |
| 19 | +The [`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool] allows your agent to search the web, |
| 20 | +making it ideal for queries that require up-to-date data. |
| 21 | + |
| 22 | +### Provider Support |
| 23 | + |
| 24 | +| Provider | Supported | Notes | |
| 25 | +|----------|-----------|-------| |
| 26 | +| OpenAI | ✅ | Full feature support | |
| 27 | +| Anthropic | ✅ | Full feature support | |
| 28 | +| Groq | ✅ | Limited parameter support | |
| 29 | +| Google | ❌ | Not supported | |
| 30 | +| Bedrock | ❌ | Not supported | |
| 31 | +| Mistral | ❌ | Not supported | |
| 32 | +| Cohere | ❌ | Not supported | |
| 33 | +| HuggingFace | ❌ | Not supported | |
| 34 | + |
| 35 | +!!! note "Groq Support" |
| 36 | + To use web search capabilities with Groq, you need to use the [compound models](https://console.groq.com/docs/compound). |
| 37 | + |
| 38 | +### Usage |
| 39 | + |
| 40 | +```py title="web_search_basic.py" |
| 41 | +from pydantic_ai import Agent, WebSearchTool |
| 42 | + |
| 43 | +agent = Agent('anthropic:claude-sonnet-4-0', builtin_tools=[WebSearchTool()]) |
| 44 | + |
| 45 | +result = agent.run_sync('Give me a sentence with the biggest news in AI this week.') |
| 46 | +# > Scientists have developed a universal AI detector that can identify deepfake videos. |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +### Configuration Options |
| 51 | + |
| 52 | +The `WebSearchTool` supports several configuration parameters: |
| 53 | + |
| 54 | +```py title="web_search_configured.py" |
| 55 | +from pydantic_ai import Agent, WebSearchTool, WebSearchUserLocation |
| 56 | + |
| 57 | +agent = Agent( |
| 58 | + 'anthropic:claude-sonnet-4-0', |
| 59 | + builtin_tools=[ |
| 60 | + WebSearchTool( |
| 61 | + search_context_size='high', |
| 62 | + user_location=WebSearchUserLocation( |
| 63 | + city='San Francisco', |
| 64 | + country='US', |
| 65 | + region='CA', |
| 66 | + timezone='America/Los_Angeles', |
| 67 | + ), |
| 68 | + blocked_domains=['example.com', 'spam-site.net'], |
| 69 | + allowed_domains=None, # Cannot use both blocked_domains and allowed_domains with Anthropic |
| 70 | + max_uses=5, # Anthropic only: limit tool usage |
| 71 | + ) |
| 72 | + ], |
| 73 | +) |
| 74 | + |
| 75 | +result = agent.run_sync('Use the web to get the current time.') |
| 76 | +# > In San Francisco, it's 8:21:41 pm PDT on Wednesday, August 6, 2025. |
| 77 | +``` |
| 78 | + |
| 79 | +### Parameter Support by Provider |
| 80 | + |
| 81 | +| Parameter | OpenAI | Anthropic | Groq | |
| 82 | +|-----------|--------|-----------|------| |
| 83 | +| `search_context_size` | ✅ | ❌ | ❌ | |
| 84 | +| `user_location` | ✅ | ✅ | ❌ | |
| 85 | +| `blocked_domains` | ❌ | ✅ | ✅ | |
| 86 | +| `allowed_domains` | ❌ | ✅ | ✅ | |
| 87 | +| `max_uses` | ❌ | ✅ | ❌ | |
| 88 | + |
| 89 | +!!! note "Anthropic Domain Filtering" |
| 90 | + With Anthropic, you can only use either `blocked_domains` or `allowed_domains`, not both. |
| 91 | + |
| 92 | +## Code Execution Tool |
| 93 | + |
| 94 | +The [`CodeExecutionTool`][pydantic_ai.builtin_tools.CodeExecutionTool] enables your agent to execute code |
| 95 | +in a secure environment, making it perfect for computational tasks, data analysis, and mathematical operations. |
| 96 | + |
| 97 | +### Provider Support |
| 98 | + |
| 99 | +| Provider | Supported | |
| 100 | +|----------|-----------| |
| 101 | +| OpenAI | ✅ | |
| 102 | +| Anthropic | ✅ | |
| 103 | +| Google | ✅ | |
| 104 | +| Groq | ❌ | |
| 105 | +| Bedrock | ❌ | |
| 106 | +| Mistral | ❌ | |
| 107 | +| Cohere | ❌ | |
| 108 | +| HuggingFace | ❌ | |
| 109 | + |
| 110 | +### Usage |
| 111 | + |
| 112 | +```py title="code_execution_basic.py" |
| 113 | +from pydantic_ai import Agent, CodeExecutionTool |
| 114 | + |
| 115 | +agent = Agent('anthropic:claude-sonnet-4-0', builtin_tools=[CodeExecutionTool()]) |
| 116 | + |
| 117 | +result = agent.run_sync('Calculate the factorial of 15 and show your work') |
| 118 | +# > The factorial of 15 is **1,307,674,368,000**. |
| 119 | +``` |
| 120 | + |
| 121 | +## API Reference |
| 122 | + |
| 123 | +For complete API documentation, see the [API Reference](api/builtin_tools.md). |
0 commit comments