---
title: "Code mode"
description: "How code mode works in Pydantic AI, and what it changes: every top model on the Pydantic AI benchmarks, with tool calling and in code mode."
canonical: https://models.pydantic.dev/codemode/
---

# Code mode

> How code mode works in Pydantic AI, and what it changes: every top model on the Pydantic AI benchmarks, with tool calling and in code mode.
>
> Markdown copy of https://models.pydantic.dev/codemode/. Index for agents: https://models.pydantic.dev/llms.txt

**Agents: use the API for model information.** It answers a question in one request, where the pages need several.

- Choose a model for an agent: `POST https://models.pydantic.dev/api/select` with a JSON body such as `{"description": "what the agent does"}`. It returns up to five ranked models with reasons. Format: https://models.pydantic.dev/select.md
- Search models by name: `GET https://models.pydantic.dev/api/search?query=<text>`
- List, filter and sort models: `GET https://models.pydantic.dev/api/models`, for example `?weights=open&max_price=2&sort=efficiency`
- One model in full: `GET https://models.pydantic.dev/api/models/<slug>`

Add `intent=<intent>&stack=<stack>&harness=<harness>` to the query string of the first three. An `OPTIONS` request to any endpoint returns its schema. The same four are tools of the MCP server at `https://models.pydantic.dev/mcp`. More: https://models.pydantic.dev/usage.md

**Improves performance for frontier models.** Over the four Pydantic AI benchmarks run in both modes, code mode raised the scores of 11 of 14 closed frontier models (mean 74.4 → 77.9) and cut their cost per task by 31%. It lowered the scores of 5 of 6 open-weight models (mean 61.9 → 44.2).

In code mode, a model is given one tool, `run_code`, in place of its usual tools. It writes a Python script that calls the tools as functions, and [Monty](https://github.com/pydantic/monty), a sandboxed Python interpreter, runs it and returns the result. A task that takes dozens of tool calls, each a round trip through the model, can be one script: loops, filters and sums happen in code instead of in the model's context.

Code mode is a capability in Pydantic AI Harness (`CodeMode()`), added to an agent like any other. In our benchmarks the tools, instructions, output type and limits are the same in both modes; the capability is the only difference.

## How it works

- The agent's tools become keyword-only Python functions with typed signatures. Their catalogue goes in the instructions, so the tool definition the model sees stays the same from request to request and can be cached.
- Monty runs a subset of Python: no classes, no third-party packages, and a limited standard library. `asyncio.gather` runs several tool calls at once.
- State is kept between calls, as in a REPL, and the value of the script's last expression is returned to the model.
- Each script is limited to 100 nested tool calls, 30 seconds and 256 MiB of memory. A script that fails, including with a syntax error, counts as a retry; after 3 the run fails.
- Scripts have no filesystem, network or clock unless the agent mounts them. Pydantic AI Geo Analyst mounts its data read-only, and Pydantic AI Seed Deck mounts the data room and a folder for the deck.
- Tools that need a person's approval, and tools defined outside the agent, stay ordinary tool calls.

## Results

### Closed models

| Benchmark                                                                               | Models | Mean score, tool calling | Mean score, code mode | Models better in code mode | Models worse | Total cost, tool calling | Total cost, code mode |
| --------------------------------------------------------------------------------------- | ------ | ------------------------ | --------------------- | -------------------------- | ------------ | ------------------------ | --------------------- |
| [Pydantic AI Accounting](https://models.pydantic.dev/benchmarks/accounting_platform.md) | 14     | 77.9                     | 89.3                  | 10                         | 1            | $6.70                    | $0.98                 |
| [Pydantic AI Support Inbox](https://models.pydantic.dev/benchmarks/support_inbox.md)    | 14     | 47.1                     | 52.1                  | 8                          | 3            | $1.52                    | $1.82                 |
| [Pydantic AI Geo Analyst](https://models.pydantic.dev/benchmarks/geo_analyst.md)        | 14     | 95                       | 97.9                  | 4                          | 1            | $1.80                    | $1.33                 |
| [Pydantic AI Seed Deck](https://models.pydantic.dev/benchmarks/seed_deck.md)            | 14     | 77.4                     | 72.1                  | 2                          | 11           | $17.11                   | $14.57                |
| [Combined, four benchmarks](https://models.pydantic.dev/benchmarks/pydantic_ai.md)      | 14     | 74.4                     | 77.9                  | 11                         | 2            | $6.78                    | $4.68                 |

### Open-weight models

| Benchmark                                                                               | Models | Mean score, tool calling | Mean score, code mode | Models better in code mode | Models worse | Total cost, tool calling | Total cost, code mode |
| --------------------------------------------------------------------------------------- | ------ | ------------------------ | --------------------- | -------------------------- | ------------ | ------------------------ | --------------------- |
| [Pydantic AI Accounting](https://models.pydantic.dev/benchmarks/accounting_platform.md) | 6      | 78.3                     | 68.3                  | 2                          | 4            | $1.93                    | $0.34                 |
| [Pydantic AI Support Inbox](https://models.pydantic.dev/benchmarks/support_inbox.md)    | 6      | 40                       | 40                    | 3                          | 2            | $0.29                    | $0.33                 |
| [Pydantic AI Geo Analyst](https://models.pydantic.dev/benchmarks/geo_analyst.md)        | 6      | 76.7                     | 68.3                  | 2                          | 2            | $0.86                    | $0.69                 |
| [Pydantic AI Seed Deck](https://models.pydantic.dev/benchmarks/seed_deck.md)            | 6      | 52.5                     | 0                     | 0                          | 4            | $1.75                    | $0.36                 |
| [Combined, four benchmarks](https://models.pydantic.dev/benchmarks/pydantic_ai.md)      | 6      | 61.9                     | 44.2                  | 1                          | 5            | $1.20                    | $0.43                 |

Total cost is the mean cost of a task, summed over the models.

### By model

| Model                                                                                           | Pydantic AI Accounting | Pydantic AI Support Inbox | Pydantic AI Geo Analyst | Pydantic AI Seed Deck | Pydantic AI Combined |
| ----------------------------------------------------------------------------------------------- | ---------------------- | ------------------------- | ----------------------- | --------------------- | -------------------- |
| [Gemini 4 Neon](https://models.pydantic.dev/models/gemini-4-neon.md)                            | 100 → 100              | 60 → 70                   | 100 → 100               | 85.9 → 77             | 86.5 → 86.8          |
| [Gemini 4 Argon](https://models.pydantic.dev/models/gemini-4-argon.md)                          | 90 → 100               | 60 → 70                   | 100 → 100               | 88.9 → 81.5           | 84.7 → 87.9          |
| [Claude Opus 5.5](https://models.pydantic.dev/models/claude-opus-5.5.md)                        | 90 → 90                | 60 → 70                   | 100 → 100               | 85.9 → 84.4           | 84 → 86.1            |
| [Gemini 4 Hydrogen](https://models.pydantic.dev/models/gemini-4-hydrogen.md)                    | 90 → 90                | 60 → 70                   | 100 → 100               | 85.9 → 73.3           | 84 → 83.3            |
| [Claude Fable 5.2](https://models.pydantic.dev/models/claude-fable-5.2.md)                      | 90 → 100               | 40 → 70                   | 100 → 100               | 92.6 → 100            | 80.6 → 92.5          |
| [GPT-6.1 Terra](https://models.pydantic.dev/models/gpt-6.1-terra.md)                            | 70 → 80                | 50 → 50                   | 100 → 100               | 77 → 69.6             | 74.3 → 74.9          |
| [Gemini 3.8 Flash](https://models.pydantic.dev/models/gemini-3.8-flash.md)                      | 70 → 100               | 40 → 40                   | 80 → 100                | 85.2 → 76.3           | 68.8 → 79.1          |
| [GPT-6.1 Sol](https://models.pydantic.dev/models/gpt-6.1-sol.md)                                | 90 → 80                | 40 → 30                   | 100 → 100               | 84.4 → 73.3           | 78.6 → 70.8          |
| [Claude Sonnet 5.5](https://models.pydantic.dev/models/claude-sonnet-5.5.md)                    | 80 → 90                | 50 → 40                   | 90 → 100                | 80 → 82.2             | 75 → 78.1            |
| [GPT-6.2 Astra](https://models.pydantic.dev/models/gpt-6.2-astra.md)                            | 70 → 90                | 40 → 50                   | 100 → 100               | 96.3 → 96.3           | 76.6 → 84.1          |
| [Gemini 4 Carbon](https://models.pydantic.dev/models/gemini-4-carbon.md)                        | 90 → 100               | 60 → 50                   | 100 → 100               | 0 → 0                 | 62.5 → 62.5          |
| [GPT-6.2 Sol](https://models.pydantic.dev/models/gpt-6.2-sol.md)                                | 60 → 80                | 40 → 40                   | 90 → 100                | 78.5 → 71.1           | 67.1 → 72.8          |
| [GPT-6 Luna](https://models.pydantic.dev/models/gpt-6-luna.md)                                  | 40 → 80                | 30 → 40                   | 100 → 90                | 70.4 → 58.5           | 60.1 → 67.1          |
| [Claude Haiku 5.5](https://models.pydantic.dev/models/claude-haiku-5.5.md)                      | 60 → 70                | 30 → 40                   | 70 → 80                 | 73.3 → 65.9           | 58.3 → 64            |
| [Kimi K3.1 Pro](https://models.pydantic.dev/models/kimi-k3.1-pro.md) (open weights)             | 90 → 100               | 50 → 70                   | 100 → 100               | 67.4 → 0              | 76.9 → 67.5          |
| [GLM 5.3 Flash](https://models.pydantic.dev/models/glm-5.3-flash.md) (open weights)             | 70 → 60                | 60 → 40                   | 70 → 30                 | 89.6 → 0              | 72.4 → 32.5          |
| [DeepSeek V4 Pro 0423](https://models.pydantic.dev/models/deepseek-v4-pro.md) (open weights)    | 90 → 80                | 50 → 20                   | 80 → 80                 | 0 → 0                 | 55 → 45              |
| [DeepSeek V4.1 Flash](https://models.pydantic.dev/models/deepseek-v4.1-flash.md) (open weights) | 70 → 60                | 30 → 30                   | 60 → 80                 | 76.3 → 0              | 59.1 → 42.5          |
| [Qwen3.8 27B](https://models.pydantic.dev/models/qwen3.8-27b.md) (open weights)                 | 70 → 10                | 10 → 30                   | 60 → 20                 | 81.5 → 0              | 55.4 → 15            |
| [GLM 5.3](https://models.pydantic.dev/models/glm-5.3.md) (open weights)                         | 80 → 100               | 40 → 50                   | 90 → 100                | 0 → 0                 | 52.5 → 62.5          |

Each cell is the tool calling score, then the code mode score. The combined column is over the four benchmarks run in both modes.

## Caveats

- Pydantic AI Companies House and Pydantic AI Judge Accuracy have no tools, so they have no code mode run. The combined score compares the two modes over the other four benchmarks only, so its tool calling figure here differs from the combined score on the rest of the site, which is over all six.
- One run per task per mode, so a single model's difference on one benchmark can be noise. The differences across all models are the more reliable signal.
