Agentic Pattern Libraryby Vikas Goyal
← All patterns
23

Code-as-Action

Also known as: code actions, programmatic tool calling, code mode

The agent writes code that chains many tool calls, instead of one JSON call per step.

TrackAgentic systems FamilyRuntime & interop Maturity Emerging ChangesTool/action
01

Problem

With classic tool calling, every call is a full model turn: call, wait, read the whole result, decide the next call. A task that touches 50 records means 50 round trips, and every intermediate result passes through the model’s context, even when the model only needs a total or a filtered list at the end.

  • Loops, filters and joins are natural in code and awkward as a series of separate calls.
  • Every round trip adds latency and tokens.
  • Large intermediate results crowd the context.
  • Running model-written code is riskier than running a fixed tool call.
02

Solution

Expose tools as functions in a code environment instead of only as JSON schemas. The model writes a short program that calls the tools, loops over results, filters and aggregates, and prints only what it needs to see. The program runs in a sandbox, and the output comes back as one result. The model reads the summary, not the raw data, and can write another program if needed. Tool permissions and budgets still apply to every call the code makes.

03

Diagram

CLASSIC · one call per turn Model call 1 call 2 call 3 … call 360 ~360 round tripsraw data in context CODE-AS-ACTION · one program Modelwrites 12 lines Sandbox loop · filter · sortover 180 accounts tools called361× in code Printed result5 rows · ~200 tokens code only the result returns to the model

One model turn replaces dozens of tool calls. Intermediate data stays in the sandbox, and only the printed result returns to the model.

04

When to use

Use it when

  • Tasks loop over many items or combine results from several tools
  • Intermediate data is large but the needed answer is small
  • Latency and token cost from many round trips are a problem
  • A sandbox is already available

Avoid it when

  • One or two tool calls do the job
  • You can't run model-written code safely
  • Every individual action needs separate human approval
05

Trade-offs

Latency One turn instead of dozens
Cost Intermediate data never enters the context
Complexity Sandbox, tool SDK and error handling for code
Reliability Exact loops and math; code bugs are the new risk

Gains

  • Far fewer model turns for bulk work
  • Exact computation instead of the model doing math in its head
  • Context stays small because raw results stay in the sandbox

Costs

  • Requires a secure sandbox for generated code
  • Code bugs can fail silently or partway through
  • Harder for reviewers to follow than a list of tool calls
06

Worked example

Scenario
A customer success agent is asked which enterprise accounts have more than 3 open P1 tickets and a renewal in the next 90 days.
  1. Classic tool callingWould need 1 call to list 180 enterprise accounts, then 180 ticket lookups and 180 CRM lookups, with each result passing through the model.
  2. Code-as-ActionThe model writes a 12-line program that lists the accounts, fetches tickets and renewal dates in batches, filters, sorts by renewal date, and prints a 5-row table.
  3. SandboxRuns the program. The ticket and CRM tools are read-only within the agent’s permissions. It makes 361 calls in total, all inside the sandbox.
  4. ModelSees only the 5-row result, about 200 tokens, and drafts a risk summary for each account.
  5. Comparison1 model turn instead of about 360, and the context grew by a table instead of thousands of ticket records.
07

Failure modes

FailureSymptomMitigation
Unsafe codeGenerated code touches files or networks it shouldn'tRun in a sandbox with tool-level permissions and no ambient credentials
Silent partial failureSome API calls fail but the program prints a clean-looking resultRequire the program to report errors and counts alongside results
Hidden actionsWrite actions buried in code bypass reviewKeep write tools behind approval even when called from code
Hard to auditReviewers can't tell what the agent actually didLog the program and every tool call it made