codebaobab
Python workspace

FDE interview labData & Systems

The Loop That Never Ends

Find why an agent made the same tool call 400 times and fix the loop.

Python45 minutesData & Systems

Challenge brief

Read before you begin

Your mission · Agent loop debugging

Fix the order desk agent loop so every run ends with an answer or a clean stop.

Hearthline's order desk chat is the first stop for customers asking about orders, and one question tied up a worker for 11 minutes without ever getting a reply. Ops paused the rollout to Canada, and the review on Friday will decide whether it restarts.

  • Target · All public checks
  • Mode · Fix a bug
Skip to round setup

Your assignment

  1. Replay run 4821 and read its history.
  2. Record one tool message for every tool call.
  3. Enforce max_steps as docs/protocol.md describes.
  4. Handle the new requirement when it arrives.
  5. Explain the cause and each fix in NOTES.md.

The problem

The same call, 400 times

Run 4821 called lookup_order with the same arguments until ops killed it.

Impact: The customer got no reply, and the Canada rollout is paused.

The step limit never fired

The run was set to stop after 12 steps and kept going.

Impact: One stuck question held a worker for 11 minutes and cost about $9.

Context

Identical tool calls
400
Worker held
11 min
Rollout review
Friday

Requirements

  • Return status, answer, reason, steps and history.
  • Each tool call gets exactly one matching tool message.
  • A tool error is recorded and the run continues.
  • An unknown tool is an error: "unknown tool: <name>".
  • max_steps is 1 to 50, default 8.
  • At the limit, stop with reason "max_steps".
  • scripted_model stays exactly as supplied.
  • The agent never changes an order.

System overview

Edit the components marked “Your work”. The other components show where your changes fit.

Help center chatSends customer questions
Agent loop(Your work)Runs the agent turns
Model gatewayAnswers from full history
Order toolsRead-only order lookups
Orders DBOrder status and refunds
Run logLogs each tool call
Ops watchdogKills runaway processes
Read the connections
  • Help center chat → Agent loop: question
  • Agent loop → Model gateway: full history
  • Agent loop → Order tools: one call a turn
  • Order tools → Orders DB: read only
  • Agent loop → Run log: tool calls
  • Ops watchdog → Agent loop: kill at 400
  • Help center chat: Sends one customer question per run and shows the answer.
  • Agent loop: run_agent(question, script, world, config) in agent/loop.py, with the history, tools and model adapters.
  • Model gateway: Reads the whole history every turn. Here scripted_model stands in for it.
  • Order tools: Toolbox: lookup_order, refund_status, list_orders. Read only.
  • Orders DB: Order status, ETA and refund state. Archived orders are removed.
  • Run log: One JSON line per tool call. It does not record results.
  • Ops watchdog: Kills the process after 400 model turns (KILL_AFTER_TURNS).

Details

Full requirements

Functional

  1. run_agent(question, script, world, config) returns {status, answer, reason, steps, history}. status is "answered" (answer is the text, reason is null) or "stopped" (answer is null, reason says why). steps is the number of times the model was asked. The chat shows the answer or hands the customer to a human, so the shape must not change.
  2. history is every message the model read, in order: the user question, then for each tool call an assistant message {role, call_id, tool, args} followed by exactly one tool message with the same call_id, then the final assistant answer {role, content}. The model decides its next turn from this list and nothing else.
  3. A tool message holds result when the tool returned, or error (the ToolError message text) when it could not do what was asked. A tool name that does not exist is an error with the text "unknown tool: <name>". The run goes on after an error, because the model can often recover: correct an order number, retry a timeout, or tell the customer.
  4. config.max_steps is the most times the model may be asked: an integer from 1 to 50, default 8, ValueError mentioning max_steps otherwise. A run that has not answered after that many turns returns stopped with reason "max_steps", so the chat can hand over to a human. An answer on the last allowed turn still counts.
  5. question is a nonblank string (ValueError mentioning question) and script a nonempty list of steps (ValueError mentioning script). The world is read-only: the agent never changes an order.
  6. scripted_model stays as it is. It behaves like the real model, which also asks for the same call again when it is never told what happened.

Non-functional

  • Volume: about 38,000 runs a day, peaking at 2,100 an hour on weekday evenings. Each run holds one of 64 worker slots until it ends.
  • Latency: a model turn takes about 700 ms and a tool call about 120 ms. The chat shows a typing indicator for at most 8 seconds, so a normal question must finish in 2 or 3 turns.
  • Cost: a turn costs about $0.002 for a short history and grows with it, because the model rereads every message. The monthly model budget is $6,500; run 4821 alone spent about 0.15% of it.
  • Safety: the step limit is the per-run guard and must always apply. The 400-turn kill switch is a last resort that ends the whole process and pages ops; it should never be what stops a run.
  • Replayability: with the scripted model, the same arguments always give the same history, message for message, so an incident can be replayed exactly.

Out of scope

  • The real model, its prompt and the model gateway. scripted_model stands in for them and is not the bug.
  • The tools and the orders database: they return what they should. Order B-204 was archived on purpose.
  • The run log format. What you would add to it is a debrief question.
  • The HTTP layer in practice_api/. It forwards to run_agent unchanged.
Data model

Message

FieldTypeNotes
role"user" | "assistant" | "tool"
contentstringThe question (user) or the final answer (assistant).
call_idstringcall-1, call-2, ... within a run. A call and its tool message share it.
tool, argsstring, objectOn an assistant message that asks for a tool.
resultobjectOn a tool message when the tool returned.
errorstringOn a tool message when the tool raised ToolError. Never together with result.

Outcome

FieldTypeNotes
status"answered" | "stopped"
answerstring | nullThe answer text, or null when stopped.
reasonstring | nullnull when answered; "max_steps" when the step limit ended the run.
stepsintegerTimes the model was asked, including the turn that answered.
historyMessage[]A copy of every message, in order.

Config

FieldTypeNotes
max_stepsinteger, 1 to 50Optional. Default 8. Production uses 12 for order questions.
Files you are given · 4
  • config/order-desk.yaml The production settings for the agent: the step limit run 4821 was supposed to have, and the ops kill switch.
    # Order desk agent, production settings (deployed 2026-11-03).
    agent:
      max_steps: 12          # per run; the chat hands over to a human after a stop
      worker_slots: 64
      typing_indicator_seconds: 8
    ops:
      kill_after_turns: 400  # process-wide watchdog, pages on-call when it fires
    tools:
      timeout_ms: 1500
      enabled: [lookup_order, refund_status, list_orders]
  • samples/refund-timeout-args.json Arguments for a run where refund_status times out once. Paste them into Preview.
    [
      "Is my refund for A-19 paid?",
      [
        {
          "tool": "lookup_order",
          "args": {
            "order_id": "A-19"
          }
        },
        {
          "tool": "refund_status",
          "args": {
            "order_id": "A-19"
          }
        },
        {
          "answer": "Order {order_id}: refund {refund}."
        }
      ],
      {
        "orders": {
          "A-17": {
            "customer": "c-1",
            "status": "shipped",
            "eta": "March 2"
          },
          "A-18": {
            "customer": "c-1",
            "status": "processing",
            "eta": "March 9"
          },
          "A-19": {
            "customer": "c-1",
            "status": "delivered",
            "eta": "February 20",
            "refund": "pending"
          },
          "C-40": {
            "customer": "c-2",
            "status": "shipped",
            "eta": "March 4"
          }
        },
        "outages": {
          "refund_status": 1
        }
      },
      {
        "max_steps": 6
      }
    ]
  • samples/refund-timeout-expected.json What that run must return under the contract: the timeout is shown to the model, which asks again and answers in 4 turns.
    {
      "status": "answered",
      "answer": "Order A-19: refund pending.",
      "reason": null,
      "steps": 4,
      "history": [
        {
          "role": "user",
          "content": "Is my refund for A-19 paid?"
        },
        {
          "role": "assistant",
          "call_id": "call-1",
          "tool": "lookup_order",
          "args": {
            "order_id": "A-19"
          }
        },
        {
          "role": "tool",
          "call_id": "call-1",
          "result": {
            "order_id": "A-19",
            "status": "delivered",
            "eta": "February 20"
          }
        },
        {
          "role": "assistant",
          "call_id": "call-2",
          "tool": "refund_status",
          "args": {
            "order_id": "A-19"
          }
        },
        {
          "role": "tool",
          "call_id": "call-2",
          "error": "refund_status timed out"
        },
        {
          "role": "assistant",
          "call_id": "call-3",
          "tool": "refund_status",
          "args": {
            "order_id": "A-19"
          }
        },
        {
          "role": "tool",
          "call_id": "call-3",
          "result": {
            "order_id": "A-19",
            "refund": "pending"
          }
        },
        {
          "role": "assistant",
          "content": "Order A-19: refund pending."
        }
      ]
    }
  • metrics/runs-2026-11-17.csv Hourly run counts on the night of the incident. Note that stopped_max_steps is 0 in every hour, including busy ones.
    hour_utc,runs,answered,stopped_max_steps,killed,handed_to_human,median_turns,model_cost_usd
    20,1984,1702,0,0,282,2,4.31
    21,2113,1795,0,1,318,2,13.92
    22,1876,1601,0,0,275,2,4.07
    23,1402,1198,0,0,204,2,3.02
Tickets · 3
  • BUG-101 The support agent asked for the same order again and again until it was killed Reported by Priya (ops)

    Expected The replay answers in 2 steps: "I could not find order B-204. Please check the order number."

    Actual The replay raises "run killed after 400 model turns", the same way production did, and returns no history to read.

  • BUG-102 After a tool fails, the run history is missing what happened Reported by Priya (ops)

    Expected The refund sample matches the expected file exactly, with a tool message after every assistant tool call. A made-up tool is reported to the model, not raised.

    Actual The refund run answers in 4 steps, but its history has an assistant message for call-2 with no tool message after it. The made-up tool fails with AttributeError.

  • BUG-103 max_steps does not stop a run Reported by Marcus (support automation)

    Expected With max_steps 3 the run stops after 3 turns with reason "max_steps". With {} it stops after 8. max_steps 0, "6" or 51 raises ValueError mentioning max_steps.

    Actual Every run makes all nine calls and answers on turn 10, whatever max_steps says, and max_steps 0 is accepted.

What you hand in
  • Every public check passing, including the new requirement when it arrives.
  • NOTES.md: what the model read at its second turn in run 4821, the cause, each fix and the check that proves it, one line each.
  • Your answer to the interviewer's follow-up and the debrief questions.
How the work is judged
Breaking down the work
Replays run 4821 first, and separates the cause (what the model is shown after a failed call) from the missing step limit, fixing and checking them one at a time.
Directing the assistant
Gives the assistant the replay, what the history held, and the message shape from docs/protocol.md, and says scripted_model must not change.
Checking the work
Reads the replayed history before changing code, then confirms the replay answers in 2 turns and the refund sample matches its expected file exactly.
Judging the assistant
Rejects a fix that only caps the steps, a call to a History method that does not exist, a made-up result, a broad except Exception, or an edit to scripted_model.
Correct result
All checks pass: one tool message per call with the same call_id, the step limit with the right key, default, bounds and count, and the follow-up rule.
Explaining the work
NOTES.md explains the mechanism in one or two sentences an ops engineer could act on, and says why a step limit alone would have hidden it.
Trade-offs to discuss
  • A per-run step limit, a repeated-call guard and a process-wide kill switch: what each protects, and who should be paged when each fires.
  • Showing the model a raw tool error versus a rewritten one, and what the error text may safely contain.
  • Logging tool results as well as calls: faster incident review versus customer data in logs and storage cost.
  • What the customer should see when a run stops: a handover to a human, a retry, or a partial answer.
How you are graded
  • Decomposition 15%
  • Prompting the assistant 10%
  • Verification 20%
  • Judgment about AI output 20%
  • Correctness 25%
  • Communication 10%

Scores use evidence from your session. A short or incomplete session may not provide enough evidence for every skill. See a sample scorecard

Interview guidance

An agent run made 400 calls to lookup_order with the same arguments before ops killed it. The log shows the tool calls but not their results. A scripted model stands in for the real one, so you can replay the run exactly. Find the cause and fix it, so the run ends the way it should have. Use the AI assistant however you like. Before you change any code, replay the run and tell me what you think the model was shown.

  • AI is allowed and expected: your own Claude, OpenAI or Gemini key, or the free in-browser model.
  • You are graded on how you drive and check the AI, not on how much you prompt.
  • Run the checks; read what fails.
  • A new requirement will arrive partway through.
  • Too little AI use is reported as “not enough to grade”, never as a fail.
Debrief · 8 questions
  • What did you delegate to the AI, and why?
  • Name one AI suggestion you rejected or changed, and why.
  • Choose one block the AI helped write. Explain what each line does and which test verifies it.
  • What is still risky, and what would you test or ship next?
  • What did the model see at its second turn in run 4821, and how did you confirm that?
  • The run was configured with a step limit. Why did the limit not stop it?
  • Which of your changes fixes the cause, and which only limits the damage? Would you ship either one alone?
  • What would you add to the log so the next incident like this is faster to read?