7 The agent loop (lesson 12b)
Hand the model four tools as OpenAI JSON schemas, then loop:
TOOL_SCHEMAS = [ lookup_cve, pip_audit_findings, search_usage, read_file ]
for round in range(MAX_TOOL_ROUNDS):
resp = client.chat.completions.create(
model=model, messages=messages,
tools=TOOL_SCHEMAS, tool_choice="auto")
msg = resp.choices[0].message
if not msg.tool_calls:
return parse_verdict(msg.content)
for tc in msg.tool_calls:
result = TOOL_IMPL[tc.function.name](**json.loads(tc.function.arguments))
messages.append({"role": "tool", "tool_call_id": tc.id, "content": result})
One OpenAI tool-calling loop, zero frameworks. 12c rewrites it as a manual ReAct loop; 12d adds retrieval.