Get AI summaries of any video or article — Sign up free
Build More Effective AI AGENTS With "Code As Action" thumbnail

Build More Effective AI AGENTS With "Code As Action"

All About AI·
5 min read

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.

TL;DR

“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?

JSON-based approaches often require one tool call per step in a workflow, and the workflow repeats for each input (e.g., each country). In the smartphone example, price lookup, currency conversion, tax estimation, and shipping estimation must be executed repeatedly, producing many separate requests. With “code as action,” the agent writes code that loops over a list of countries and computes final prices inside the code, so the repeated pattern happens within one executed action rather than across many separate tool-call steps.

How did the agent estimate projected API prices for 2025 in the walkthrough?

It first used a web/visit tool to gather inputs such as the “DeepSeek R1” API price, the “Google Gemini 2.0” API price, and the historical degree of cost reduction over the last two years. Then the code agent performed calculations: it used numerical values for current prices and applied an assumed historical cost reduction (around 49–50% in the run) to project next-year per-million-token costs. The final output included projected prices like 0.48 for DeepSeek R1 and 0.19 for Flash API (as shown in the results).

What role do loops play in the “code agent” approach?

Loops are the mechanism that replaces repeated external tool calls. Instead of generating separate action sequences for each country (or each item), the agent places items into a list and uses a for-loop to iterate through them, computing results for each element using the available tools and functions. This is why the workflow can be executed as one action even when the underlying logic repeats many times.

How were errors handled during code execution?

When the agent encountered an error, it fed the error message back into the next round of code generation. The run used a maximum step limit (set to 10 in the walkthrough), allowing retries until the agent produced a successful final answer.

How did the custom Matplotlib tool work, and what guardrail was emphasized?

A custom tool was created that imports Matplotlib, generates a plot from provided data, and saves the output as a PNG file in the working directory (the walkthrough mentions saving to a PNG file in the CVD). The presenter emphasized “authorized imports” for the code agent, restricting which third-party libraries the agent can import—reducing the risk of malicious or unsafe code execution.

What was the practical demonstration of the custom tool’s value?

After creating the tool, the agent used it to generate a chart visualizing projected API price changes. The resulting graph showed price per million tokens on the left axis and model names along the bottom, including a plotted current price and projected price for the models. The key point was that the plot generation and saving happened within a single action rather than multiple sequential tool-call steps.

Review Questions

  1. 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?
  2. 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?
  3. 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. 1

    “Code as action” collapses repetitive multi-tool workflows into one executed code action instead of many sequential JSON tool calls.

  2. 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. 3

    Fewer tool calls can translate into lower latency and reduced overhead, which also reduces the operational cost of running agents.

  4. 4

    A code agent can combine web-fetched inputs with in-code calculations to produce forward-looking estimates like 2025 API pricing.

  5. 5

    Custom tools can extend agent capabilities—for example, generating and saving Matplotlib charts as PNG files.

  6. 6

    Restricting what the code agent is allowed to import (“authorized imports”) provides a practical safety guardrail for executing generated code.

Highlights

The smartphone pricing demo shows how a for-loop over countries can replace dozens of sequential JSON tool calls.
Projected 2025 API prices were produced by combining web-sourced current prices with an assumed historical cost reduction (~49–50%).
A custom Matplotlib tool enabled the agent to generate a PNG chart of projected API price changes, with safety supported by restricted imports.

Topics