Save To File

Save agent responses to a file automatically.

1"""
2Save To File
3=============================
4
5Save agent responses to a file automatically.
6"""
7
8import os
9
10from kern.agent import Agent
11from kern.models.openai import OpenAIResponses
12
13# ---------------------------------------------------------------------------
14# Create Agent
15# ---------------------------------------------------------------------------
16agent = Agent(
17 model=OpenAIResponses(id="gpt-5.2"),
18 save_response_to_file="tmp/agent_output.md",
19 markdown=True,
20)
21
22# ---------------------------------------------------------------------------
23# Run Agent
24# ---------------------------------------------------------------------------
25if __name__ == "__main__":
26 os.makedirs("tmp", exist_ok=True)
27 agent.print_response(
28 "Write a brief guide on Python virtual environments.",
29 stream=True,
30 )
31 print(f"\nResponse saved to: {agent.save_response_to_file}")

Run the Example

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