Reasoning Agent Events
Stream agent events including run lifecycle, tool calls, and content output.
Reasoning Agent Events.
1"""2Reasoning Agent Events3=============================45Reasoning Agent Events.6"""78import asyncio910from kern.agent import RunEvent11from kern.agent.agent import Agent12from kern.models.openai import OpenAIResponses1314# ---------------------------------------------------------------------------15# Create Agent16# ---------------------------------------------------------------------------17finance_agent = Agent(18 model=OpenAIResponses(id="gpt-5.2"),19 reasoning=True,20)212223async def run_agent_with_events(prompt: str):24 content_started = False25 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}")3233 if run_output_event.event in [RunEvent.reasoning_started]:34 print(f"\nEVENT: {run_output_event.event}")3536 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: ignore3940 if run_output_event.event in [RunEvent.reasoning_completed]:41 print(f"\nEVENT: {run_output_event.event}")4243 if run_output_event.event in [RunEvent.run_content]:44 if not content_started:45 print("\nCONTENT:")46 content_started = True47 else:48 print(run_output_event.content, end="")495051# ---------------------------------------------------------------------------52# Run Agent53# ---------------------------------------------------------------------------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 repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python reasoning_agent_events.py