Reasoning Agent Events

Stream agent events including run lifecycle, tool calls, and content output.

Reasoning Agent Events.

1"""
2Reasoning Agent Events
3=============================
4
5Reasoning Agent Events.
6"""
7
8import asyncio
9
10from kern.agent import RunEvent
11from kern.agent.agent import Agent
12from kern.models.openai import OpenAIResponses
13
14# ---------------------------------------------------------------------------
15# Create Agent
16# ---------------------------------------------------------------------------
17finance_agent = Agent(
18 model=OpenAIResponses(id="gpt-5.2"),
19 reasoning=True,
20)
21
22
23async def run_agent_with_events(prompt: str):
24 content_started = False
25 async for run_output_event in finance_agent.arun(
26 prompt,
27 stream=True,
28 stream_events=True,
29 ):
30 if run_output_event.event in [RunEvent.run_started, RunEvent.run_completed]:
31 print(f"\nEVENT: {run_output_event.event}")
32
33 if run_output_event.event in [RunEvent.reasoning_started]:
34 print(f"\nEVENT: {run_output_event.event}")
35
36 if run_output_event.event in [RunEvent.reasoning_step]:
37 print(f"\nEVENT: {run_output_event.event}")
38 print(f"REASONING CONTENT: {run_output_event.reasoning_content}") # type: ignore
39
40 if run_output_event.event in [RunEvent.reasoning_completed]:
41 print(f"\nEVENT: {run_output_event.event}")
42
43 if run_output_event.event in [RunEvent.run_content]:
44 if not content_started:
45 print("\nCONTENT:")
46 content_started = True
47 else:
48 print(run_output_event.content, end="")
49
50
51# ---------------------------------------------------------------------------
52# Run Agent
53# ---------------------------------------------------------------------------
54if __name__ == "__main__":
55 task = (
56 "Analyze the key factors that led to the signing of the Treaty of Versailles in 1919. "
57 "Discuss the political, economic, and social impacts of the treaty on Germany and how it "
58 "contributed to the onset of World War II. Provide a nuanced assessment that includes "
59 "multiple historical perspectives."
60 )
61 asyncio.run(
62 run_agent_with_events(
63 task,
64 )
65 )

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/14_advanced
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python reasoning_agent_events.py