14. Interview Questions & MCQs
AI Agent Coding Interview Questions
Questions and answers
- Sketch a minimal agent tool loop in code terms.
Answer While under max_steps: call the model with messages + tool schemas; if it returns tool_calls, validate/authorize/execute each and append tool results as observations; else return the final assistant message and stop.
- Where should tool execution live — in the LLM or in your app?
Answer In your app. The model only proposes a name and arguments; your runtime validates allowlists/schemas, checks authZ, runs the function, and feeds the observation back.
- How do you define a tool for an LLM?
Answer Expose a JSON schema: name, description, and typed parameters (required/optional). Clear descriptions improve selection; strict schemas catch bad args before execution.
- How do you validate tool arguments safely?
Answer Parse with a schema library (Pydantic/Zod), reject unknown fields, coerce types carefully, and never eval raw strings. Fail closed with a structured error observation the model can recover from.
- What is structured output and why use it in agents?
Answer Force the model to return JSON (or a schema) for plans, tool calls, or final answers so your code can parse reliably instead of scraping free text.
- How do you prevent infinite agent loops in code?
Answer Enforce max_steps, wall-clock timeout, token/cost budget, and a no-progress detector (same tool+args repeating). On breach, stop or escalate to HITL with a summary.
- How should retries work for tool failures?
Answer Retry transient 5xx/timeouts with backoff and jitter; do not blindly retry 4xx. Cap attempts, log each try, and return a clear error observation after exhaustion.
- Idempotency — why does it matter for agent tools?
Answer Agents may retry or re-run steps. Writes should use idempotency keys or safe upserts so duplicate tool calls do not double-charge, double-email, or corrupt state.
- How do you implement short-term memory in code?
Answer Keep a message list (system, user, assistant, tool). Trim or summarize when near the context limit; never drop the system policy or the latest tool results without a plan.
- How do you implement long-term memory?
Answer Persist facts/episodes in a store (SQL/vector), retrieve top-k relevant items per turn, inject as context, and gate writes behind validation and tenant ACL. Support forget/delete APIs.
- Show the shape of a ReAct-style turn.
Answer Thought (optional) → Action(tool, args) → Observation(result) → … → Final Answer. In APIs this is usually assistant tool_calls + tool role messages, not free-form “Thought:” text alone.
- How do you sandbox shell or code-execution tools?
Answer Run in an isolated container/VM with CPU/memory/time limits, filesystem allowlists, no host mounts by default, blocked network or egress allowlist, and non-root users.
- How do you keep secrets out of the prompt and logs?
Answer Inject secrets only inside the tool runtime via env/secret manager — never into model messages. Redact tokens, keys, and PII in traces before storage.
- How would you code HITL for a risky tool?
Answer Mark tools as requires_approval. When proposed, pause the run, persist pending state, notify a human, and resume only with an approved/denied decision recorded in the audit log.
- Parallel tool calls — when and how?
Answer If the model returns multiple independent tool_calls, execute with asyncio.gather / Promise.all under a concurrency limit. Do not parallelize dependent steps; preserve result order in messages.
- How do you unit-test an agent loop without calling a real LLM?
Answer Fake the model client with scripted responses (tool_call then final answer). Assert which tools ran, args validated, stop rules fired, and final output matches — deterministic and cheap.
- How do you integration-test tools?
Answer Hit real or contract-tested doubles for each tool: happy path, schema violation, auth failure, timeout. Verify observations are serializable and safe for the next model turn.
- How do you stream agent UX while tools run?
Answer Stream assistant tokens for interim text; emit structured events for tool_start/tool_end; keep the client UI in sync with run state. Never stream secrets or raw stack traces.
- What belongs in an agent run record (DB row)?
Answer run_id, user/tenant, status, step count, model, token/cost totals, error, timestamps, and pointers to message/tool spans. Enough to resume, debug, and audit.
- How do you version prompts and tool schemas?
Answer Store prompt/tool schema versions with the run. Change schemas behind compatibility checks so old runs remain explainable and evals stay comparable.
- Write vs read tools — how do you separate them in code?
Answer Tag tools with side_effect: read|write. Auto-allow reads under ACL; route writes through stricter policy, rate limits, and optional HITL. Never give the model a god-mode admin client.
- How do you handle partial JSON / malformed tool calls?
Answer Prefer provider structured/tool calling. If parsing fails, return a tool error observation asking for valid JSON; after N failures, stop. Do not execute best-effort garbage args.
- How would you implement plan-and-execute in code?
Answer First model call returns a list of steps (schema). Execute steps in order with tools; after each step, optionally replan on failure. Persist plan state so you can resume.
- How do you cap cost in a coding agent?
Answer Track tokens and tool spend per run; abort when budget exceeded; prefer smaller models for routing; cache embeddings/retrieval; limit max_steps and max parallel tools.
- What is a good error observation format?
Answer Structured JSON: {ok:false, code, message, retryable}. Keep messages short and actionable so the model can correct args without dumping stack traces or secrets.
- How do you prevent prompt injection via tool output?
Answer Treat tool/web/email content as untrusted data. Wrap it in delimiters, never concatenate into the system prompt, and keep tool allowlists so injected text cannot grant new powers.
- Async Python vs Node for agent backends — what matters?
Answer Both work if I/O is non-blocking. Use async HTTP for model/tools, bound concurrency (semaphores), timeouts on every call, and cancel runs cleanly on client disconnect.
- How do you expose an agent over HTTP?
Answer Authenticated route creates a run, accepts input, returns run_id; poll or SSE/WebSocket for events; server owns secrets and tool execution. Never put API keys in the browser.
- What tests would you write for a coding agent that edits files?
Answer Fixture repo, scripted model patches, assert file diffs, tests/linters run, stop on green or budget; reject path traversal and writes outside the workspace allowlist.
- Give a crisp opener for “Implement an agent tool loop.”
Answer “I’d write a bounded loop: model → validate tool calls → execute under allowlist → append observations → repeat until final answer or budget; log every step with redaction.”