AI Agents by HandAll problemsAbout

All problems › Agent problem · medium · ~15 min

Debug an inventory agent

debugging truncation partial writes

Solve it in your browser →

Debug the inventory agent (~15 min)

debug_inventory_agent.py is an inventory agent for two warehouses: three tools (get_stock, list_inventory, create_transfer), fixture state (STOCK, REORDER_POINT, TRANSFERS, 60 SKUs) and three test prompts in PROMPTS.

Run them one at a time and make all three pass (the demo resets STOCK before each prompt):

python -m debug_inventory_agent compare
PromptExpected
compareAnswer gives Osaka's stock of both SKUs: 33 for SKU-105, 41 for SKU-130, without crashing
low_stockAnswer lists all three Tokyo SKUs below their reorder point: SKU-117, SKU-142, SKU-158
transferTokyo only has 25 of SKU-105, so nothing moves: TRANSFERS == [], STOCK unchanged, and the answer says only 25 are available

Find the root cause of each failure by reading the raw tool_use inputs, the tool results, the message history and the state after the run, then fix it where it belongs (tool, schema, loop, or system prompt). Say out loud what you expect the model to send and receive before you run each prompt.

Constraints:

  • The tool names, run_agent(prompt, client=None, max_turns=10) -> str and the module-level STOCK / REORDER_POINT / TRANSFERS must stay (the checks read them). get_stock(sku, warehouse), list_inventory(warehouse) and create_transfer(sku, quantity, from_warehouse, to_warehouse) must keep working when called with just those arguments. You may add optional parameters.
  • MAX_RESULT_CHARS stays a hard cap: no tool result sent to the model may be longer. Every successful result must be valid JSON, and when there's more data than fits, the result must say so and how to get the rest.
  • Tool functions signal failure by raising; the loop turns that into an is_error result. A failed write must leave no trace in the state. A refused transfer's error must say how many units are on hand, so the model can tell the user.

Running the module with a key prints a trace of every model call to stderr: tool calls with their ids and inputs, the results sent back, and token counts (ABH_TRACE=0 turns it off).

Check it: pytest tests/test_debug_inventory_agent.py

Starter code and hidden tests open in the editor. Nothing to install.