Skip to content
ML to LLMs
Cheat sheets

Agents and tool use

Tool calling, the agent loop, token growth, MCP, workflow patterns, reliability math, and guardrails on one page.

Tool call lifecycle: tool schemas in the prompt → model emits a structured call (constrained decoding keeps it schema-valid) → runtime validates, authorizes, executes → result appended as a tool message → model called again.

Agent loop (ReAct): observe the context → reason → act (tool call or final answer) → observe the result. Stops on a final answer or a runtime limit (max steps, token or cost budget, timeout, repeated call).

FormulaMeaning
ct=B+∑s<t(os+rs)c_t = B + \sum_{s<t}(o_s + r_s)input tokens read by call tt
Cin=TB+d T(T−1)/2C_{\text{in}} = TB + d\,T(T-1)/2total billed input for TT calls, dd tokens added per step
S=pnS = p^ntask success, nn independent steps at per-step success pp (0.95 over 10 steps ≈ 0.60)
peff=1−(1−p)[(1−ρ)+ρ(1−p)r]p_{\text{eff}} = 1 - (1-p)[(1-\rho) + \rho(1-p)^r]per-step success with a check of recall ρ\rho and rr retries
pass@k=1−(1−s)k\text{pass@}k = 1-(1-s)^kat least one of kk trials succeeds (capability)
passk=sk\text{pass}^k = s^kall kk trials succeed (reliability)
PatternUse when
Prompt chainingthe task splits into known stages
Routinginputs fall into distinct types
Parallelizationsubtasks are independent, or you want votes
Orchestrator-workerssubtasks are decided at run time
Evaluator-optimizerthere are clear quality criteria to iterate against
Autonomous agentthe steps can't be written down in advance

MCP: the host app runs MCP clients; each connects to an MCP server (stdio locally, HTTP remotely, JSON-RPC messages) that exposes tools, resources, and prompts. Write an integration once, use it from any compliant client. Server output is still untrusted.

Memory: short-term = the context (exact, bounded, re-billed every step). Long-term = a store the agent writes to and retrieves from (RAG's failure modes apply).

Tool design: narrow, specific names; descriptions say when not to use the tool; few typed arguments; idempotent writes with request keys; compact results; errors that say what to do next.

Guardrails: least privilege with the user's credentials; validate arguments and business rules in code; human approval for irreversible actions (show the evidence); sandbox code execution; per-task step, spend, and per-tool limits.

Evaluate: fixed task suite in a reproducible environment; final-state checks plus trajectory assertions; several runs per task; report per-trial success and pass^k; cost and latency per task (median, p95); adversarial cases (injection in tool results, failing tools).

Failure modes: loops, wrong tool from vague descriptions, hallucinated tools or IDs, context overflow, prompt injection through tool results, false success claims.

Cost levers: fewer steps (billed input grows roughly with T2T^2), prefix caching (stable prompt and tools, append-only history), compact tool results, parallel tool calls, smaller models for subtasks.

Lessons: Tool use and agents · Building reliable agents · Securing AI systems