Loading patternsโฆ
Code as Action (CodeAct)(CA)
Instead of emitting one JSON tool call per turn, the agent's action space is executable code that orchestrates tools with loops, conditionals, and variables. A single code block can call many tools, branch on results, and keep intermediate data in the runtime rather than passing every result back through the context window. Anthropic reports roughly a 98 percent token reduction on some multi-tool workflows when tools are called from code via MCP.
In 30 seconds
- What
- Agent writes executable code that calls multiple tools with loops, conditionals, and variables in a single turn, keeping intermediate results in runtime.
- When to use
- Multi-tool workflows with iteration, branching, or state that would otherwise require many sequential model round-trips and context window passes.
- Watch out
- Code execution errors halt the entire workflow; agent must debug and retry, and sandboxing overhead can negate token savings on simple tasks.
Ask the AI expert about this pattern
Opens the assistant with your question prefilled. You review it before sending.
Code as Action (CodeAct): Overview
Instead of emitting one JSON tool call per turn, the agent's action space is executable code that orchestrates tools with loops, conditionals, and variables. A single code block can call many tools, branch on results, and keep intermediate data in the runtime rather than passing every result back through the context window. Anthropic reports roughly a 98 percent token reduction on some multi-tool workflows when tools are called from code via MCP.
- Action space is executable code, not a single JSON call
- Loops, conditionals, and variables compose many tool calls per turn
- Intermediate results stay in the runtime, not the context window
- Fewer model round-trips for multi-step, multi-tool work
- Large token reduction on tool-heavy workflows (up to ~98 percent reported)
- Tools exposed to code (for example via MCP) with results filtered before return
The Agent Architect
One pattern, one tradeoff, one production failure story. A short weekly briefing for people building agentic systems.
Weekly email, one-click unsubscribe. We only use your address to send the briefing.
References
The papers, specifications, and repositories this pattern is based on.
From the engineer behind this catalog
Get your agent architecture reviewed
This page documents one pattern. Your system runs dozens, and most failures live in how they fit together. Have the whole design reviewed against the 288 patterns in this catalog: architecture, reliability, evaluation and cost, every finding mapped to the pattern that fixes it.
โฌ750 instead of โฌ1,500, one week, written report and walkthrough call, until 30 September