Agents, Tool Use & MCP·Question 63 of 90Advanced

Instead of exposing 130 tools as function definitions, you expose them as a code API the agent writes scripts against. Walk me through the tradeoffs.

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Two wins, then the costs, which are the real content.

Full worked answer with diagram

Two wins, then the costs, which are the real content.

Definitions load on demand. Present the tools as typed modules on a filesystem, servers/salesforce/updateRecord.ts and so on, and the agent lists directories and reads only the handful it needs. Schema cost then scales with what the task touches rather than with what you connected. Anthropic's write-up on this reports an example dropping from roughly 150,000 tokens to 2,000, about 98.7%.

Intermediate results stay in the sandbox. Fetch 10,000 rows, filter to five in code, return five. With function calls all 10,000 land in context and are re-sent on every later iteration, so the saving compounds (Q17). This is usually the larger win, and it doubles as a privacy boundary: PII can be masked before anything reaches the model.

Control flow resolves in one turn. A loop over 50 records is one script, not 50 model calls, with retries and conditionals as code rather than as trajectory. Fewer iterations means less compounding error and less latency.

Now the costs:

  • You need a genuine sandbox. Resource limits, wall-clock timeouts, default-deny egress, no ambient credentials (Q42). Arbitrary code moves the enforcement boundary from a tool dispatcher you wrote to a runtime you now have to operate.
  • Approval gates get harder. Per-action approval was easy when the unit was a typed tool call. A script is one approval for an opaque sequence, and no operator can code-review it at speed. Keep the gate at the capability layer instead: the API functions the script calls go through a broker that enforces policy per operation, so approval and rate limiting happen where the side effect happens, not where the script is submitted.
  • Errors arrive as stack traces. A TypeError twelve frames deep is a far worse recovery signal than "invalid status, valid values: open, closed" (Q15), so wrap and re-shape errors deliberately.
  • Traces become programs. "Which tool did it call at step 12" now needs instrumentation inside the sandbox: emit a span per brokered call, not just the script text. Replay means re-running code against a world that may have moved, a weaker guarantee than replaying a rendered prompt (Q46, Q51).

Where I draw the line: a code API for large tool surfaces, large results and batch work; direct function calls for a small tool set, latency-sensitive turns, and single irreversible actions I want gated individually.

Worth sketching. Both paths side by side separate the schema saving from the result saving.

flowchart LR
    A["130 tools"] -->|"function definitions, all upfront"| B["~150k tokens of schema"]
    A -->|"typed modules on a filesystem"| C["Agent reads only what it needs"]
    C --> D["Script runs in a sandbox"]
    D -->|"10k rows filtered to 5"| E["Small result enters context"]
    B --> F["Every result round-trips<br/>and is re-sent each iteration"]

What a good answer covers

  • Two wins, then the costs, which are the real content
  • Present the tools as typed modules on a filesystem, servers/salesforce/updateRecord.ts and so on, and…
  • Schema cost then scales with what the task touches rather than with what you connected
  • Anthropic's write-up on this reports an example dropping from roughly 150,000 tokens to 2,000, about 98.7%
  • Intermediate results stay in the sandbox

Key terms: Definitions load on demand · You need a genuine sandbox · Approval gates get harder · Errors arrive as stack traces · Traces become programs

Interviewer follow-ups to expect

  1. At how many tools, or how large a result, does the code API start paying for itself?

  2. What does a human approval prompt actually look like when the unit of action is a script?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep