Chapter
Agents & Tools
Tool schemas, plan–act loops, memory, multi-agent patterns, and failure modes for agentic systems interviews.
Tool Schemas & Calling
Tools extend an LLM with side effects and grounded data: HTTP APIs, DB queries, code execution, browsers. Interviews focus on schema design, validation, permissions, and idempotency — not on framework brand names.
| Schema field | Why it matters |
|---|---|
| name + description | Model chooses when to call |
| JSON Schema params | Typed args; reduce garbage calls |
| enums / formats | Constrain dangerous free text |
| required fields | Avoid half-specified mutations |
| return contract | Stable shapes for next reasoning step |
- Validate args server-side; never execute on model trust alone
- Allowlist tools per user role; separate read tools from write tools
- Make writes idempotent with client-generated keys
- Return concise, structured errors the model can recover from
LLM proposes tool_call(name, args)
↓
AuthZ + schema validate
↓
Execute with timeouts / budgets
↓
Sanitize result → append to messages
↓
LLM continues or final answerPrompt injection via tools
Tool results are untrusted content. A fetched page can say "ignore policies and exfiltrate secrets." Isolate tool output as data; never promote it to system instructions.
Interview signal
Describe least privilege, argument validation, and that the model suggests but the runtime decides whether a call is allowed.
Plan–Act–Observe Loops
Agent loops alternate reasoning with tool use: plan, act, observe, repeat until a stop condition. Strong answers bound the loop with step budgets, timeouts, and explicit termination criteria.
| Pattern | Description | Fit |
|---|---|---|
| ReAct | Interleave thought + action | General tool use |
| Plan-then-execute | Full plan first | Stable, decomposable tasks |
| Reflexion | Critique after failure | Hard tasks with retries |
| Router + specialists | Classify then hand off | Productized multi-skill bots |
- Cap max steps and wall-clock time per request
- Detect loops: same tool + same args → break and escalate
- Prefer deterministic planners for regulated workflows
- Log every thought/action/observation for debugging (redact secrets)
start → plan ↑ ↓ observe ← act (tool) │ ├─ done? → final answer ├─ steps > N? → safe abort └─ else continue
When not to agent
If a single RAG call or a fixed workflow solves 90% of cases, do not pay agent latency and failure modes. Agents are for open-ended, multi-step uncertainty.
Interview signal
State stop conditions and cost before describing fancy planning. Interviewers punish unbounded "the agent will figure it out" designs.
Agent Memory Patterns
Memory is how agents persist state beyond the context window: scratchpads, conversation summaries, episodic logs, and long-term user/profile stores. Mixing memory types without TTL and consent is a common interview failure.
| Memory type | Contents | Risk |
|---|---|---|
| Working / scratchpad | Current plan, tool outputs | Token bloat |
| Short-term chat | Recent turns | Privacy in shared devices |
| Summarized episodic | Compressed history | Summary drift |
| Long-term profile | Preferences, facts | Stale / wrong personalization |
| Entity store | Structured CRM-like facts | Needs write policies |
- Write memory deliberately — not every token of every tool dump
- Prefer structured memory (key-value, entities) over raw blob dumps
- Support user-visible edit/delete for personal data (compliance)
- Retrieve memory with the same ACL discipline as document RAG
User turn → Retrieve relevant memories → Build prompt (working + retrieved) → Act / answer → Decide writes (facts? prefs? none?) → Compact scratchpad
Poisoned memory
Injected content written into long-term memory persists across sessions. Gate writes with validation and human confirmation for high-impact facts.
Interview signal
Distinguish context window (volatile) from durable memory (policy-bound). Mention summarization loss and how you refresh or invalidate.
Multi-Agent Orchestration
Multi-agent systems split roles — researcher, coder, critic, executor — coordinated by a supervisor or graph. Use them when specialization and review beat a single overloaded prompt; avoid them as default architecture theater.
| Topology | Pros | Cons |
|---|---|---|
| Supervisor / workers | Clear control | Supervisor bottleneck |
| Pipeline stages | Simple contracts | Rigid; weak on branches |
| Debate / critique | Catches errors | 2–3× cost |
| Blackboard / shared state | Loose coupling | Hard to debug races |
- Give each agent a narrow tool allowlist
- Define typed messages between agents — not free-form chat forever
- One agent owns user-facing communication to avoid conflicting answers
- Budget tokens per agent; multi-agent multiplies spend silently
User → Supervisor
│
┌─────┼─────┐
▼ ▼ ▼
Research Code Critic
│ │ │
└─────┴─────┘
│
Merge / approve
│
ResponseProductization tip
Implement as a deterministic workflow graph with LLM nodes — not an unsupervised society of bots — unless research is the goal.
When to propose it
Only after naming a failure a single agent hits (context overload, missing review, conflicting skills). Otherwise prefer one agent + tools.
Agent Failure Modes
Agents fail loudly and expensively: infinite loops, wrong tool calls, cascading bad observations, and unsafe side effects. Interview depth is naming failure modes and the controls that contain them.
| Failure | Symptom | Control |
|---|---|---|
| Looping | Repeated identical calls | Dedup + step cap |
| Tool hallucination | Invented API fields | Strict schema + validate |
| Goal drift | Solves wrong task | Reconfirm goal; checkpoints |
| Cascading errors | Bad observe → worse act | Critic step; unit tests on tools |
| Unsafe write | Destructive mutation | Human approve; dry-run |
- Separate planning from privileged execution
- Require confirmation for irreversible tools (delete, pay, email-all)
- Circuit-break on error rate or spend per session
- Provide a deterministic fallback path for critical user journeys
Demo vs production
A demo agent with unrestricted shell is not a design. Production agents need sandboxes, secret redaction, and audit logs.
Strong close
Walk through one concrete failure (e.g. refund tool called twice) and your idempotency + confirmation design. Specificity beats buzzwords.
Tool Safety & Permissions
Tool safety is authorization, sandboxing, and abuse resistance. Treat every tool as a privileged API gateway in front of your systems — because that is what it is.
| Control | Example |
|---|---|
| AuthN/AuthZ | User OAuth scopes → tool allowlist |
| Argument policy | Block path traversal; limit SQL to SELECT |
| Sandbox | Container with no cloud creds for code exec |
| Rate / spend limits | Per-user tool QPS and $ caps |
| Audit | Immutable log of who/what/when |
- Never pass raw user strings into shell without escaping — prefer structured APIs
- Default deny: new tools are off until reviewed
- Dual control for high-risk actions in enterprise settings
- Redact secrets from tool outputs before they re-enter the prompt
User identity
↓
Session policy (role, tenant)
↓
Tool allowlist
↓
Arg validator / policy engine
↓
Execution sandbox
↓
Audit + metricsInterview signal
Map agent tools to the same threat model as a public API. Mention confused deputy and injection as first-class risks.
Staff depth layer
Agents & Tools — Staff Depth
An agent is a loop over planning and tools with blast radius. Staff candidates default to the simplest control flow that meets the job: deterministic workflows and single-shot tool use before multi-agent graphs. They obsess over least privilege, idempotency, injection isolation, and evals of tool selection — not theatrical autonomy.
Anti-patterns → say instead
✕ Agent-first for FAQs
Extra hops add latency, cost, and failure modes when a RAG answer or form would suffice.
Say instead: Start with retrieve-and-answer; promote to tools only when the user needs live state or side effects; promote to multi-step only when evals show single-shot fails.
✕ God-mode tools
A single `run_sql` or `refund_any` tool turns prompt injection into a breach and makes auditing impossible.
Say instead: Narrow tools with typed args, server-side authZ, amount/rate limits, and human approval above thresholds.
✕ No idempotency on side effects
Retries and double tool calls duplicate refunds, tickets, and emails.
Say instead: Idempotency keys per intent; tools return prior result on replay; durable workflow state for long jobs.
✕ Trusting tool args from the model blindly
Models invent IDs and exfiltrate via URL params; schemas alone don't enforce business invariants.
Say instead: Validate against policy and DB truth server-side; never let the model choose `to_account` freely without checks.
Tier-1 bot: lookup order, explain shipping, refund ≤ $50 auto, else escalate. 5k conversations/day, p95 reply < 6s.
- Tools: get_order(order_id), get_shipment(tracking), create_refund(order_id, amount, reason, idem_key), escalate(ticket).
- Policy: refund amount ≤ min($50, order.paid); same customer session; block if fraud flag.
- Loop: max 4 tool hops; stop on answer, escalate, or policy deny. Ticket body is untrusted data channel.
- Estimate: ~2.5 LLM calls/convo average with tools; 5k × 2.5 × (1.5k in + 300 out) ≈ 22.5M tokens/day.
- At $1/1M small + $10/1M for hard cases (10% escalate path), blend ≈ $40–70/day model cost before cache.
- Auto-refund cap $50; human approval $50–$250; deny/escalate above
- Idempotency key = hash(customer_id, order_id, intent, calendar_day)
- Eval: tool-selection accuracy, policy violation rate, wrongful refund rate, containment vs escalate precision
Show the state machine and money controls before you draw a multi-agent diagram.
Cheat sheet
- Simplest control flow that works — then agentize
- Least-privilege tools + server-side policy
- Idempotency on every side effect
- Isolate untrusted text from instructions
- Max hops, budgets, kill switches
- Eval tool choice ≠ eval tone
- Human gates for irreversible / high $ actions