from praisonaiagents import Agentagent = Agent( instructions="You are a helpful writer", output="poem.txt")agent.start("Write a poem about nature")# ✅ Output saved to poem.txt
With template formatting:
Copy
from praisonaiagents import Agentfrom praisonaiagents.config import OutputConfigagent = Agent( instructions="You are a helpful writer", output=OutputConfig( output_file="poem.txt", template="# {{title}}\n\n{{content}}" ))agent.start("Write a poem about nature")# ✅ Output saved to poem.txt
Agent decides when and what to save:
Copy
from praisonaiagents import Agentfrom praisonaiagents.tools import write_fileagent = Agent( name="Writer", instructions="Write content and save it to the specified file", tools=[write_file])agent.start("Write a poem and save it to poem.txt")
Auto-save task result to file:
Copy
from praisonaiagents import Agent, Task, Agentsagent = Agent(name="Writer")task = Task( description="Write a poem about nature", agent=agent, output_file="poem.txt", create_directory=True)agents = AgentManager(agents=[agent], tasks=[task])agents.start()
Capture and save manually:
Copy
from praisonaiagents import Agentagent = Agent(name="Writer")response = agent.start("Write a poem")with open("poem.txt", "w") as f: f.write(response)
from praisonaiagents import Agentagent = Agent( instructions="You are a helpful writer", output="output/response.txt")agent.start("Write a poem about nature")# ✅ Output saved to output/response.txt
from praisonaiagents import Agentagent = Agent( instructions="You are a researcher", output="reports/summary.md")agent.start("Summarize AI trends")# ✅ Creates reports/ directory if needed# ✅ Output saved to reports/summary.md
The agent automatically creates parent directories if they don’t exist.
from praisonaiagents import Agentfrom praisonaiagents.config import OutputConfigagent = Agent( instructions="You are a blog writer", output=OutputConfig( output_file="blog_post.md", template="""# {{title}}{{content}}---*Generated by AI*""" ))agent.start("Write about Python programming")
The template instructs the agent to format its response accordingly. Both OutputConfig.template and TemplateConfig.response work - use TemplateConfig.response when you don’t need file saving.
agent = Agent( instructions="You are a researcher", output=OutputConfig( verbose=True, # Show rich terminal output output_file="research.md", # Also save to file template="## Summary\n\n{{content}}" ))
from praisonaiagents import Agentfrom praisonaiagents.tools import write_fileagent = Agent( name="ReportWriter", role="Technical Writer", goal="Create and save technical documentation", tools=[write_file])# Agent will use write_file tool to save the reportagent.start("Write a technical report about Python and save it to report.md")
from praisonaiagents import Agentfrom praisonaiagents.tools import read_file, write_file, list_filesagent = Agent( name="FileManager", instructions="Manage files: read, write, and organize", tools=[read_file, write_file, list_files])agent.start("Read config.json, update the version to 2.0, and save as config_new.json")
The agent autonomously decides when to call write_file based on the task.
from praisonaiagents import Agent, Task, Agents# Create agentwriter = Agent( name="ContentWriter", role="Writer", goal="Create engaging content")# Task with output_filetask = Task( description="Write a blog post about machine learning", expected_output="A well-structured blog post in markdown", agent=writer, output_file="blog_post.md", create_directory=True # Creates parent directories if needed)# Runagents = AgentManager(agents=[writer], tasks=[task])result = agents.start()# File automatically saved to blog_post.md
from praisonaiagents import Agentimport jsonagent = Agent(name="DataProcessor")response = agent.start("Generate a list of 5 programming languages with descriptions")# Save as textwith open("languages.txt", "w") as f: f.write(response)# Or save as JSON (if response is structured)with open("languages.json", "w") as f: json.dump({"content": response}, f, indent=2)
from praisonaiagents import Agentfrom pathlib import Pathagent = Agent(name="Writer")response = agent.start("Write a short story")output_path = Path("stories/short_story.txt")output_path.parent.mkdir(parents=True, exist_ok=True)try: output_path.write_text(response) print(f"✅ Saved to {output_path}")except IOError as e: print(f"❌ Failed to save: {e}")
from praisonaiagents import Agentfrom praisonaiagents.tools import write_file, read_fileagent = Agent( name="DocumentProcessor", role="Document Specialist", goal="Process and save documents efficiently", tools=[write_file, read_file])
3
Run and Save
Copy
# Agent will automatically save to the specified fileresult = agent.start(""" Write a project README with: - Project title: My AI Project - Description - Installation steps - Usage examples Save it to README.md""")print("✅ Document saved!")