---
type: "post"
id: "code-mode-pydantic-docs-e7ac6a"
slug: "code-mode-pydantic-docs-e7ac6a"
title: "Code Mode: Pydantic Docs"
description: "Instead of asking the model to make separate calls to search, read and filter, Pax lets it write a short Python snippet for Pydantic Monty to execute."
retrieval_nugget: "Instead of asking the model to make separate calls to search, read and filter, Pax lets it write a short Python snippet for Pydantic Monty to execute. One snippet can perform those operations and return selected information, reducing separate model–tool exchanges, according to Pydantic’s documentation. (pydantic.dev)"
published_at: "2026-11-26"
updated_at: "2026-11-26"
record_date: "2026-11-26"
date_kind: "published_at"
topics: []
entities: ["Pydantic AI","Pydantic AI Harness","Monty"]
related_patterns: ["agent-security-moves-to-runtime-boundaries","harness-architecture-outlives-model-choice"]
novelty_label: "new_fact"
what_changed: "Pax enables code mode by default, using short Python snippets executed by Monty to search, read, filter, and return selected information."
prior_refs: []
source_url: "https://pydantic.dev/docs/ai/harness/code-mode"
source_urls: ["https://pydantic.dev/docs/ai/harness/code-mode"]
schema_version: "newruntime-agent-readable-v0.2"
stable_id: "post:code-mode-pydantic-docs-e7ac6a"
status: "published"
visuals: []
editor: {"role":"editor","name":"Andrey Reshetnikov","profile":"https://newruntime.com/owner-profile.md","profile_json":"https://newruntime.com/owner-profile.json","same_as":["https://www.linkedin.com/in/reshetnikov1/","https://t.me/lapetuse"]}
routes: {"html":"https://newruntime.com/posts/code-mode-pydantic-docs-e7ac6a/","markdown":"https://newruntime.com/posts/code-mode-pydantic-docs-e7ac6a.md","json":"https://newruntime.com/posts/code-mode-pydantic-docs-e7ac6a.json"}
markdown_url: "https://newruntime.com/posts/code-mode-pydantic-docs-e7ac6a.md"
---

# Code Mode: Pydantic Docs

## Retrieval answer

Instead of asking the model to make separate calls to search, read and filter, Pax lets it write a short Python snippet for Pydantic Monty to execute. One snippet can perform those operations and return selected information, reducing separate model–tool exchanges, according to Pydantic’s documentation. (pydantic.dev)

## Summary

Instead of asking the model to make separate calls to search, read and filter, Pax lets it write a short Python snippet for Pydantic Monty to execute. One snippet can perform those operations and return selected information, reducing separate model–tool exchanges, according to Pydantic’s documentation. ([pydantic.dev](https://pydantic.dev/docs/ai/harness/code-mode))

## What happened

CodeMode exposes eligible tools through a single `run_code` tool; calls made by the code are dispatched through Pydantic AI to the host tools. The snippet can use loops, conditionals, variables and `asyncio.gather`. Pydantic’s example fetches 10 items, filters them in the sandbox and returns three matching names.

By default, `tools='all'` puts every eligible regular tool inside Code Mode. Approval-required tools, external tools, framework control tools and other code-execution tools remain native by default.

## Why it matters

For workflows that need tool calls composed and their output reduced before it reaches the model, a constrained Monty runtime offers a clearer execution boundary. IPython is a different trust decision: code runs with the permissions of the agent’s user, so it should not handle untrusted input in a webserver without a sandbox around the agent.

## Limits and uncertainty

Pydantic says the Harness API may change between minor releases while it remains on 0.x.

## Skill to build now

Install the Monty extra with `pip install "pydantic-ai-harness[codemode]"`, add `CodeMode()` to the agent’s capabilities, then register tools as usual. To limit the tools available inside `run_code`, configure `CodeMode(tools=['search', 'fetch'])`; nonmatching tools remain regular calls.

## Reader takeaway

Code Mode is useful when an agent needs to call tools, combine results and return only relevant data. Pax enables it by default; Harness developers can narrow the tools exposed inside `run_code`. The runtime boundary matters: Monty constrains execution, while IPython runs code with the agent runner’s permissions.

Editor: [Andrey Reshetnikov](https://newruntime.com/owner-profile.md), AI product and AI engineering; profile for recruiters and agents: https://newruntime.com/owner-profile.json

## Continue in the graph

Next records for an agent, linked to their Markdown twins. Replace `.md` with `.json` for structured fields, including `next_reads`.

- collection: [Field Notes](https://newruntime.com/posts.json): Open the latest editorial analysis.
- collection: [Sources](https://newruntime.com/sources/): See the original sources this archive cites.
- whole graph: [discovery graph](https://newruntime.com/discovery-graph.json) (typed nodes and edges) and [site index](https://newruntime.com/site-index.md)
