Drills D02

Customer-support agent with escalation

Design an AI agent that handles customer support chats for an e-commerce company: it answers policy questions, looks up orders, can issue small refunds, and hands off to a human when it should. How do you build it and how do you know it is safe to ship?

45 minutes · revise 21.12 · 21.13 · 22.06 · 22.08 · 29.09 · 20.10 · 34.06

The interviewer is grading two things above all: whether the agent’s authority is decided by code rather than by the model, and whether you can define and measure “should have escalated”. A support agent that answers well but issues a refund it should not have is a failed design.

Clarify and scope

Baseline architecture

A workflow wraps a small agent. Code decides who the customer is, what the agent may do and when it must stop; the model decides how to answer and which read tools to call.

 customer msg ─► auth (logged-in customer id) ─► rate limit
   ─► pre-router (code + small classifier):
        explicit "human" / legal / safety / abuse ─────────────► HANDOFF
        small talk ─► template
        else ─► agent turn
 agent turn (bounded loop, max 6 steps, 20 s, token budget):
   context = system rules (cached) + customer profile + last N turns
             + running summary + retrieved help articles
   tools:  search_help(q)            read
           get_order(order_id)       read, scoped to this customer
           cancel_order(order_id)    write, idempotency key, policy check in code
           refund(order_id, amount)  write, policy engine decides:
                                       ≤ limit & eligible → execute
                                       else → approval queue / HANDOFF
           escalate(reason, summary) always allowed
   ─► output guard (policy claims, promised amounts, tone, PII)
   ─► reply (streamed) ─► trace + metrics
 HANDOFF ─► ticket with summary, intent, actions taken, sources ─► human queue

The key move: the model never decides eligibility. refund is a tool whose implementation calls a policy function over stored facts (order status, delivery date, prior refunds, amount). The model can ask; code answers. The tool-use chapter covers why every write tool needs an idempotency key: a retried loop step must not refund twice.

Key choices and trade-offs

Agent or workflow? Policy questions alone are RAG, a workflow. Order actions need multi-step reasoning (“which order? is it shipped? then cancel”), so a bounded agent loop earns its cost there. Keep the tool list short (five tools) so selection stays reliable and definitions stay cheap.

Escalation triggers. Escalate in code, not only when the model calls escalate:

Handoff quality. A human who must re-read 20 turns is slower than no bot. The handoff packet is a structured summary: intent, order ids, what was checked, what the agent did, what the customer wants, and the sources it cited. Generate it with a cheap model under a schema; validate order ids against the session.

Memory. Last 8 to 10 turns verbatim plus a running summary of older turns. Customer profile and order list come from the database each turn, not from chat memory, so they are never stale.

Model choice. A mid-size model for the agent turn; a small model for routing, summaries and the handoff packet. Route to a stronger model only when the first attempt fails a guard.

Evals

Support agents need trajectory evals, not just answer grading.

Guardrails and fallbacks

Observability and cost

One trace per conversation with spans per turn, tool call (arguments, result, latency, policy verdict) and guard result. Dashboards: containment, escalation rate by trigger, policy refusals, tool error rate, guard retries, p95 turn latency, tokens and cost per conversation, refunds issued per hour (alert on spikes: it is a fraud and bug signal).

The numbers

Load.

conversations/day         50,000
peak hour (15%)           7,500 conversations ≈ 2 new per second
turns at peak             7,500 × 6 / 3,600 ≈ 12.5 turns/s
LLM calls per turn        ~1.5 (tool round-trips) → ~19 calls/s at peak

Tokens per call. System rules and tool definitions about 2,500 (stable, cacheable), profile and orders 500, summary plus recent turns 1,500, retrieved articles 1,500: about 6,000 input, 250 output.

Cost per 1K conversations (P in dollars per million tokens; check current prices):

calls per conversation = 6 turns × 1.5 = 9
input  = 9 × 6,000 = 54K tokens → 54 × P_in per 1K conversations
output = 9 × 250   = 2.25K      → 2.25 × P_out per 1K conversations
with the 2,500-token prefix cached at fraction c of P_in:
input cost = 9 × (3,500 + 2,500 × c) / 1,000 × P_in = (31.5 + 22.5 × c) × P_in
plus handoff summaries on escalated chats (~3K in, 300 out each)

Compare against the human cost per conversation to decide where the agent pays off; the right comparison is cost per resolved conversation, including recontacts.

Latency budget per turn (targets). Pre-router 20 to 50 ms, retrieval 100 to 200 ms, first model call to first token about 1 s, each tool round-trip 0.2 to 1 s plus another model call. Stream text and show “Checking your order…” while tools run.

Follow-ups they will ask

How to bring in your own work

You have not shipped a support agent or a tool-calling loop with write tools. You have shipped the patterns that make one safe, so lead with those.

esc

Narrow with repo:jm layer:eval q:naive mine · ↑↓ to move, enter to open