Local File System Tools
Write and read files in a target directory with LocalFileSystemTools, including a write-only agent that disables read_file.
Demonstrates local file system tools for reading from and writing to files. The agent can create files and read their contents using the local file system.
"""
Local File System Tools
========================
Demonstrates local file system tools for reading from and writing to files.
The agent can create files and read their contents using the local file system.
"""
from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.tools.local_file_system import LocalFileSystemTools
# ---------------------------------------------------------------------------
# Create Agents
# ---------------------------------------------------------------------------
# Example 1: Read and write files (default)
agent = Agent(
model=OpenAIResponses(id="gpt-5.5"),
tools=[LocalFileSystemTools(target_directory="tmp/local_file_system")],
markdown=True,
)
# Example 2: Write-only mode (disable read_file)
write_only_agent = Agent(
model=OpenAIResponses(id="gpt-5.5"),
tools=[
LocalFileSystemTools(
target_directory="tmp/local_file_system", enable_read_file=False
)
],
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# Write a file and then read it back
agent.print_response(
"Write a short poem about programming to a file named poem.txt, then read it back to me.",
stream=True,
)
# Write-only agent: generate code and save it to disk
# write_only_agent.print_response(
# "Write a Python function that calculates fibonacci numbers and save it to tmp/local_file_system/fib.py",
# stream=True,
# )Paths are relative to target_directory. In the commented write-only prompt, ask to save as fib.py; repeating tmp/local_file_system/fib.py can create that directory structure again underneath the base. Writes replace existing text files of the same name. enable_read_file=False removes the read tool; no Python code is executed merely by saving it.
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 local_file_system_tools.py, then run:
python local_file_system_tools.pyFull source: cookbook/91_tools/local_file_system_tools.py