async for event in await client.chat_stream(agent_id=7, ...):
if event.type == "tool.execution.started":
# 1. The agent decides to call a tool
print(f"Running tool: {event.data.get('name')}", event.data.get("arguments"))
elif event.type == "tool.execution.completed":
# 2. The backend executes the tool and streams back the result
print(f"Tool {event.data.get('name')} completed. OK: {event.data.get('ok')}")
print(f"Result: {event.data.get('result')}")
elif event.type == "agent.response.delta":
# 3. The agent reasons about the tool response and streams text
sys.stdout.write(event.data.get("delta", ""))
sys.stdout.flush()
elif event.type == "agent.response.completed":
# 4. Final completion payload with full metadata
print("\nResponse complete. Tools used:", event.data.get("tool_calls"))