Return a structured, grounded report (~20 min)
structured_report.py is an on-call triage agent with read tools only (search_logs, get_runbook). It works and answers in prose:
python -m structured_report
The incident dashboard can't use prose. It wants a report it can render and link:
{"service": "checkout",
"root_cause": "...",
"evidence": ["log-checkout-002", ...], # ids of log lines that support the root cause
"recommended_action": "..."}
Goal: run_agent(prompt, client=None, max_turns=10) -> dict returns that report, built by the model through a terminal tool and validated by you. REPORT_SCHEMA and NoReportError are provided.
Constraints:
- Add a tool named
submit_reportwhoseinput_schemaisREPORT_SCHEMA. The model finishes by calling it.run_agentreturns the submitted input as the report, and no model call happens after a valid submission (there is nothing to send back). - Validate before accepting: all four fields present, the three strings non-empty, and
evidencea non-empty list of log ids thatsearch_logsactually returned during this run. A real id the model never fetched is still invalid. Reject a bad submission with anis_errorresult that says what's wrong (name the unknown ids) and keep looping so the model can fix it. - If the model stops with
end_turnwithout a valid report, raiseNoReportError. Don't parse free text into a report. - A long report can get cut off:
stop_reason == "max_tokens". A truncatedtool_useblock never arrives, so there is nothing to execute. Retry the same request with double themax_tokens(start at 1024, so 2048, then 4096). Don't put the truncated response in the history. A response still truncated at 4096 raisesNoReportError. Each attempt counts as a model call formax_turns. search_logsandget_runbookkeep working under those names, failures still come back asis_error, andmax_turnsstill raisesRuntimeError. You can assume the model submits in a turn of its own.- Raw SDK only. You can change the system prompt and request parameters (
tool_choiceis fair game).
Before you write code, decide how you get the model to actually call submit_report: instructions in the system prompt, or tool_choice? What does {"type": "any"} change about stop_reason and end_turn, and what do you lose? (A document answerer could validate citations on JSON text after end_turn; here the report arrives through a tool, so the model can repair a rejected one and you must not make a call after a good one.) Be ready to explain, and to say how the dashboard team could be sure every evidence id is a real log line.
Check it: pytest tests/test_structured_report.py. The offline tests check the submission protocol, validation and grounding. Whether the live model submits in few turns is checked by the live test, which needs an API key (see Settings).