DS DevShelfHub Projects · AI tools
Tutorials / CrewAI / Reference / Classes / FileWriterTool
Class tools

FileWriterTool: Reference Guide

By DevShelfHub

Writes content to a local file path.

See the CrewAI API reference index, CrewAI introduction, and core concepts for surrounding context.

What is FileWriterTool?

FileWriterTool gives agents the ability to persist generated artifacts — reports, CSV extracts, or intermediate JSON — directly to disk. That convenience is also the risk: any path the model can guess becomes writable unless you constrain it. Treat this tool as privileged: wrap it with hooks, run agents under dedicated OS users with chrooted directories, or subclass to enforce canonical destinations per job ID.

Unlike object storage uploads, local writes are synchronous and easy to audit in development, but they do not survive autoscaling events unless volumes are mounted. For SaaS multi-tenant workloads, prefer presigned S3 uploads or a custom BaseTool that writes through your API with authz checks.

Combine with FileReadTool when agents need iterative refinement: read baseline, transform, write new version with a monotonically increasing filename so retries never clobber prior attempts.

When to Use

Persisting results to disk.

Use Cases

  • Report generation
  • Local pipelines

Key Features

  • Trivial setup

When NOT to Use

Multi-tenant SaaS — use object storage.

Notes

Atomic writes

Agents may crash mid-write. Use temp files plus os.replace or write-to-string then flush once to avoid half-written JSON consumers read.

Permissions

If the process user cannot chmod output directories, failures surface as tool errors the LLM will retry endlessly. Pre-create directories with correct ownership in your entrypoint.

Secrets in output

Models sometimes echo API keys into reports. Scan FileWriterTool output in CI for high-entropy strings before publishing artifacts.

Disk quotas

Long-running crews can fill disks when each task writes debug blobs. Rotate filenames or prune job directories after successful kicks.

Import

python
from crewai_tools import FileWriterTool

Code Examples

Minimal wiring

python
from crewai import Agent
from crewai_tools import FileWriterTool

agent = Agent(role='Writer', goal='Save drafts', backstory='Always write UTF-8.', tools=[FileWriterTool()])

Register with other constrained tools

python
from crewai import Agent
from crewai_tools import FileWriterTool

agent = Agent(role='Archiver', goal='Write reports to the job folder', backstory='Never leave the sandbox path.', tools=[FileWriterTool()])

Paired read/write analyst

python
from crewai import Agent
from crewai_tools import FileReadTool, FileWriterTool

agent = Agent(
    role='ETL helper',
    goal='Normalize files',
    backstory='Read source, write cleaned output next to it.',
    tools=[FileReadTool(), FileWriterTool()],
)

Common Mistakes

❌ Allowing arbitrary write paths

✅ Constrain via a hook or wrapper.

FileWriterTool FAQ

What is FileWriterTool in CrewAI?

Writes content to a local file path. FileWriterTool gives agents the ability to persist generated artifacts — reports, CSV extracts, or intermediate JSON — directly to disk. That convenience is also the risk: any path the model can guess becomes writable unless you constrain it. Treat this tool as privileged: wrap it with hooks, run agents under dedicated OS users with chrooted directories, or subclass to enforce canonical destinations per job ID. Unlike object storage uploads, local writes are synchronous and …

Which package defines the CrewAI class FileWriterTool?

DevShelfHub maps FileWriterTool to Python module crewai_tools (package path crewai_tools in this reference). Pin your installed crewai version and match imports to the snippet on this page.

When should I use FileWriterTool?

Persisting results to disk.

When should I avoid using FileWriterTool?

Multi-tenant SaaS — use object storage.

How do I import FileWriterTool in Python?

from crewai_tools import FileWriterTool

Where can I explore more CrewAI API reference pages?

Open the CrewAI API reference index on DevShelfHub to search 58 classes, 30 methods, and 16 decorators, each with runnable examples, parameters, common mistakes, and cross-links.