Skip to content

Agent tools

Token-cheap code tools for agents: view, search, repo map, patch, diagnostics and the tool cache.

Every method shows the same operation in each SDK; pick a language once and every tab on the site follows it.

Python TypeScript Go Ruby Java

sandbox.tools. cwd is where relative paths start (the guest’s default working directory when None). With a cache (a ToolCache, which forks can share) the tools it names answer from it; each method’s cache argument overrides that.

Python exposes these as methods; see below.

def apply_patch(self, patch: str | None=None, *, edits: list[dict[str, Any]] | None=None, dry_run: bool=False, diagnostics: bool=False, verify: str | None=None, budget: int | None=None, cwd: str | None=None, cache: bool | None=None) -> ToolResult

Apply a unified diff, or edits ({path, old, new} replaces text that occurs once; {path, content} creates a file; {path, delete: True} removes one). All or nothing: on any conflict no file changes and text reports each problem with the closest text in the file. diagnostics=True then runs the language server on each changed file it knows. verify (a test or build command, e.g. "pytest -q") then runs in cwd and its failures are appended, so an edit and its check are one call; the patch stays applied whatever it says, and data["verify"] holds its exit code, summary and failed tests.

Parameter Type Default
patch str | None None
edits list[dict[str, Any]] | None None
dry_run bool False
diagnostics bool False
verify str | None None
budget int | None None
cwd str | None None
cache bool | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:175
def command(self, command: str, *, pure: bool=False, env: dict[str, str] | None=None, cwd: str | None=None, timeout_seconds: int=600, budget: int | None=None) -> ToolResult

Run a shell command. text is the exit code, what a test runner or compiler reported (summary, failed tests, error locations, and what changed since the last run of the same command here), then the output cut to the budget with the end of the log and the lines that report errors kept first. pure=True declares that its output depends only on the files under the cache root, the command and env (no network, clock or randomness), so with a cache it answers from it; a pure command that changes the tree is not stored. data holds exit_code, stdout and stderr (each up to 1 MiB: the start and the end of a longer stream), and runner, summary, failed and errors.

Parameter Type Default
command str
pure bool False
env dict[str, str] | None None
cwd str | None None
timeout_seconds int 600
budget int | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:198

Not in the Python SDK’s AgentTools.

def diagnostics(self, path: str, *, language: str | None=None, budget: int | None=None, cwd: str | None=None, timeout: int=60, cache: bool | None=None) -> ToolResult

Errors first, then warnings, for one file, from the language server for language (guessed from the extension).

Parameter Type Default
path str
language str | None None
budget int | None None
cwd str | None None
timeout int 60
cache bool | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:189

Not in the Python SDK’s AgentTools.

def repo_map(self, path: str='.', *, focus: list[str] | None=None, mentions: list[str] | None=None, max_files: int | None=None, budget: int | None=None, cwd: str | None=None, cache: bool | None=None) -> ToolResult

The repository’s most referenced definitions, one source line each, as many as fit in budget (1024 by default). focus (files the agent is working on, relative to path) and mentions (names) steer the ranking.

Parameter Type Default
path str '.'
focus list[str] | None None
mentions list[str] | None None
max_files int | None None
budget int | None None
cwd str | None None
cache bool | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:167
def run(self, tool: str, args: dict[str, Any], *, budget: int | None=None, cwd: str | None=None, timeout_seconds: int=120, cache: bool | None=None) -> ToolResult

Run one tool by name with the driver’s own arguments (see sdk/surface/agent_tools/__main__.py).

Parameter Type Default
tool str
args dict[str, Any]
budget int | None None
cwd str | None None
timeout_seconds int 120
cache bool | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:108
def search(self, pattern: str, path: str='.', *, glob: str | list[str] | None=None, fixed_strings: bool=False, case: str='smart', context: int=0, max_matches: int=50, max_per_file: int=10, budget: int | None=None, cwd: str | None=None, cache: bool | None=None) -> ToolResult

Matches grouped by file with line numbers, at most max_matches in all and max_per_file per file, and the totals.

Parameter Type Default
pattern str
path str '.'
glob str | list[str] | None None
fixed_strings bool False
case str 'smart'
context int 0
max_matches int 50
max_per_file int 10
budget int | None None
cwd str | None None
cache bool | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:159
def upload(self) -> None

Write the tools into the sandbox. run does this by itself the first time a sandbox lacks them.

Source: sdk/python/svm/agent_tools.py:148
def view(self, path: str, *, start: int=1, lines: int=100, line_chars: int | None=None, budget: int | None=None, cwd: str | None=None, cache: bool | None=None) -> ToolResult

Lines start to start + lines - 1 of a file, numbered, with how many lines lie above and below; a directory is listed.

Parameter Type Default
path str
start int 1
lines int 100
line_chars int | None None
budget int | None None
cwd str | None None
cache bool | None None

Returns ToolResult

Source: sdk/python/svm/agent_tools.py:153
def with_cache(self, cache: ToolCache | None) -> AgentTools

These tools behind cache (None turns it off).

Parameter Type Default
cache ToolCache | None

Returns AgentTools

Source: sdk/python/svm/agent_tools.py:104
Python TypeScript Go Ruby Java

What a tool returns. text is what to show the agent; ok is False when the tool could not do what was asked (a missing file, a patch that does not apply) and text then says why. tokens is estimated at three characters a token, which errs high. cached is True when the answer came from the tool-result cache. For command, data holds exit_code, stdout and stderr.

Property Type Description
cached bool
data dict[str, Any]
error str | None
ok bool
text str
tokens int
truncated bool
Python TypeScript Go Ruby Java

The answers, in memory (max_entries, least recently used first out) or in directory so processes can share them.

tools are the agent tools cached when a call does not say (cache=None); a call passes cache=True or False to opt in or out itself. apply_patch is cached only as a dry run, and a command only when the call says pure=True. root is the tree digested, relative to the call’s working directory; exclude names directories (at any depth) left out of it.

Give the tools a working directory (AgentTools(sandbox, cwd="/work", cache=...) or cwd= per call): without one the sandbox’s is /, which is not cached (the call runs uncached and says why). A tree too large to digest within the driver’s limits (200,000 entries or 20 seconds) is not cached either.

Python exposes these as methods; see below.

def get(self, key: str, digest: str) -> dict[str, Any] | None
Parameter Type Default
key str
digest str

Returns dict[str, Any] \| None

Source: sdk/python/svm/tool_cache.py:115
def clear(self) -> None
Source: sdk/python/svm/tool_cache.py:151

Not in the Python SDK’s ToolCache.

Not in the Python SDK’s ToolCache.

def key(self, tool: str, args: dict[str, Any], budget: int | None, cwd: str | None, version: str) -> str

The call’s key without the digest: tool, inputs, budget, the tools’ version and the tree’s bounds. The tools answer in paths relative to the root, so where the tree sits is left out; a command can print it, so its working directory is in.

Parameter Type Default
tool str
args dict[str, Any]
budget int | None
cwd str | None
version str

Returns str

Source: sdk/python/svm/tool_cache.py:92
def known(self, key: str) -> list[str]
Parameter Type Default
key str

Returns list[str]

Source: sdk/python/svm/tool_cache.py:104

Not in the Python SDK’s ToolCache.

def put(self, key: str, digest: str, answer: dict[str, Any], seconds: float) -> None
Parameter Type Default
key str
digest str
answer dict[str, Any]
seconds float
Source: sdk/python/svm/tool_cache.py:128
def record(self, *, hit: bool=False, miss: bool=False, stored: bool=False, uncacheable: bool=False, saved: float=0.0) -> None
Parameter Type Default
hit bool False
miss bool False
stored bool False
uncacheable bool False
saved float 0.0
Source: sdk/python/svm/tool_cache.py:156
def request(self, key: str) -> dict[str, Any]

The cache field the guest program reads.

Parameter Type Default
key str

Returns dict[str, Any]

Source: sdk/python/svm/tool_cache.py:100

Not in the Python SDK’s ToolCache.

def wants(self, tool: str, args: dict[str, Any], choice: bool | None) -> bool
Parameter Type Default
tool str
args dict[str, Any]
choice bool | None

Returns bool

Source: sdk/python/svm/tool_cache.py:85
Python TypeScript Go Ruby Java

saved_seconds adds, for every hit, how long the stored run took less how long the hit took.

Property Type Description
hits int
misses int
saved_seconds float
stored int
uncacheable int
hit_rate: float

Property (read-only).