Save To File
Save agent responses to a file automatically.
"""
Save To File
=============================
Save agent responses to a file automatically.
"""
import os
from agno.agent import Agent
from agno.models.openai import OpenAIResponses
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
save_response_to_file="tmp/agent_output.md",
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
os.makedirs("tmp", exist_ok=True)
agent.print_response(
"Write a brief guide on Python virtual environments.",
stream=True,
)
print(f"\nResponse saved to: {agent.save_response_to_file}")Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openaiExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as save_to_file.py, then run:
python save_to_file.pyFull source: cookbook/02_agents/02_input_output/save_to_file.py