Build More Effective AI AGENTS With "Code As Action"
Based on All About AI's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
“Code as action” collapses repetitive multi-tool workflows into one executed code action instead of many sequential JSON tool calls.
Briefing
“Code as action” lets AI agents do multi-step tool work in one shot by generating and running code (including loops), instead of chaining dozens of separate JSON tool calls. The practical payoff is fewer requests, lower latency, and less tool-call overhead—especially when the same pattern repeats across many inputs.
The walkthrough starts with a cost-optimization example for buying a smartphone model (“code act one”). In a traditional tool-calling approach, the agent runs tools sequentially: it looks up phone prices, converts currencies, estimates taxes, and adds shipping—repeating that whole chain for each candidate country. Even for a small set, the process balloons into many steps and multiple tool calls per country, creating a lot of intermediate requests.
The alternative uses a “code agent” approach where the model writes code that performs the same logic in a loop. Countries are placed into a list, and a for-loop iterates through them to compute the final price for each option. Instead of generating many separate JSON blobs (and running many sequential tool calls), the agent executes the logic as one consolidated code action. The presenter highlights that code actions are more concise than JSON-based action sequences and can collapse what would be “parallel streams” of tool calls into a single step.
Beyond the smartphone demo, the testing shifts to a more analytical task: estimating future API pricing for 2025. A custom setup uses a web tool (via a small agents library) to fetch information like the “DeepSeek R1” API price, “Google Gemini 2.0” API price, and the degree of cost reduction over the last two years. Then the code agent performs calculations—using historical cost reduction (about a 49–50% figure in the run) and applying it to current average prices—to produce projected per-million-token costs for the next year. When errors occur, the agent can retry up to a configured maximum number of steps, feeding error messages back into subsequent code generation.
The final experiment demonstrates extending the agent with a custom tool. Using documentation and an “authorized imports” concept, the presenter builds a tool that uses Matplotlib to visualize projected API price changes and save the output as a PNG file. The code agent then calls this new tool to generate a graph of price per million tokens across models, again emphasizing that the whole workflow can be executed in one action rather than many sequential tool-call steps.
Overall, the key takeaway is operational: “code as action” turns repetitive, multi-tool workflows into loop-driven code execution, which can reduce the number of tool calls, speed up results, and make complex agent behaviors easier to express—while still allowing guardrails through restricted imports for custom tools.
Cornell Notes
“Code as action” changes how AI agents use tools by letting the model generate and run code (including loops) in a single action, rather than chaining many sequential JSON tool calls. In the smartphone pricing example, looping over a list of countries replaces repeated tool-call sequences for each country. In a second test, the agent pulls current API prices and historical cost-reduction data from the web, then performs calculations to project 2025 per-million-token costs. A third test extends the agent with a custom Matplotlib-based tool that saves a PNG chart, showing how restricted “authorized imports” can control what code the agent is allowed to run. The practical impact is fewer requests, lower latency, and more compact action planning.
Why does “code as action” reduce tool-call overhead compared with JSON-based tool chaining?
How did the agent estimate projected API prices for 2025 in the walkthrough?
What role do loops play in the “code agent” approach?
How were errors handled during code execution?
How did the custom Matplotlib tool work, and what guardrail was emphasized?
What was the practical demonstration of the custom tool’s value?
Review Questions
- In the smartphone pricing example, what specific repeated steps created many tool calls under JSON-based chaining, and how did the loop-based code approach avoid that repetition?
- What inputs did the agent gather from the web before calculating 2025 API price projections, and how was historical cost reduction applied to current prices?
- How do “authorized imports” and a custom tool definition work together to make code-agent tool use safer while still enabling visualization with Matplotlib?
Key Points
- 1
“Code as action” collapses repetitive multi-tool workflows into one executed code action instead of many sequential JSON tool calls.
- 2
Loop-driven code (e.g., iterating over a list of countries) replaces repeated tool-call chains and can cut down the number of requests.
- 3
Fewer tool calls can translate into lower latency and reduced overhead, which also reduces the operational cost of running agents.
- 4
A code agent can combine web-fetched inputs with in-code calculations to produce forward-looking estimates like 2025 API pricing.
- 5
Custom tools can extend agent capabilities—for example, generating and saving Matplotlib charts as PNG files.
- 6
Restricting what the code agent is allowed to import (“authorized imports”) provides a practical safety guardrail for executing generated code.