Reference · Snippet · python
Minimal agent loop (Python)
Last updated
MAX_STEPS = 5
messages = [{"role": "user", "content": goal}]
for _ in range(MAX_STEPS):
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=tools,
)
msg = resp.choices[0].message
if not msg.tool_calls:
return msg.content # final answer
messages.append(msg)
for call in msg.tool_calls:
result = run_tool(call.function.name, call.function.arguments)
messages.append({"role": "tool", "tool_call_id": call.id, "content": result})