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

How do you engineer an agent for cost and latency without wrecking quality?

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

Optimise the trajectory, not the call - the metric that matters is cost per successful task (and time-to-completion), because a cheap model that fails 30% more can cost multiples once retries and abandoned runs are counted.

Full worked answer

Optimise the trajectory, not the call - the metric that matters is cost per successful task (and time-to-completion), because a cheap model that fails 30% more can cost multiples once retries and abandoned runs are counted.

The levers, roughly by impact:

  • Model tiering per step. Frontier model for planning, ambiguous decisions, and final synthesis; a small fast model for mechanical steps - routing, extraction, summarising tool results, compaction. Many trajectories are 20% hard steps and 80% mechanical ones; tiering routinely cuts spend severalfold. Subagents make this natural: workers on cheaper models, orchestrator on a strong one.
  • Prompt caching. Agent loops are the ideal caching workload: system prompt + tool definitions + growing history prefix are identical on every iteration; only the tail changes. Cached input is billed at a steep discount, commonly 50-90% off uncached input depending on provider and model (some providers add a cache-write premium, others cache automatically) - on a 20-iteration loop this is the single largest cost lever, often bigger than model choice. Structure prompts stable-prefix-first so appends don't invalidate the cache.
  • Context discipline. Every retained token is re-billed each iteration (cached or not, it's still latency and attention). Token-efficient tool results, clearing stale results, and compaction compound across iterations.
  • Fewer iterations. Consolidated tools and parallel tool calls collapse round trips; deterministic glue in code instead of model steps removes them entirely. Each avoided iteration saves a whole context replay.
  • Latency-specific moves: parallelize independent tool executions; stream partial output and show tool activity so perceived latency drops even when total time doesn't; overlap tool execution with speculation cautiously; keep hot paths on faster models.
  • Caching above the model: memoize deterministic tool results (same search query within a session), reuse subagent findings across a batch of related tasks.

Guard the quality side with your eval suite: tiering and compaction changes are exactly the kind that silently degrade pass^k, so every cost optimisation ships with an eval run, and step-count/token metrics are tracked as first-class regression signals.

What a good answer covers

  • Optimise the trajectory, not the call - the metric that matters is cost per successful task (and…
  • Model tiering per step
  • Prompt caching
  • Context discipline
  • Fewer iterations

Key terms: cost per successful task · Model tiering per step · Prompt caching · Context discipline · Fewer iterations · Latency-specific moves · Caching above the model

Interviewer follow-ups to expect

  1. How do you decide which steps are safe for the cheap model - static rules or learned routing?

  2. Why can aggressive history truncation increase cost?

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