Files

Read and write files with agents

Agents can work with files – read documents, write outputs, and manage data.

Diagram

File Operations

👤 User

🤖 Agent

📄 Read

✏️ Write

📁 Manage

Quick Start

  1. Simple Usage
    typescript
    import { Agent } from 'praisonai';
    
    const agent = new Agent({
      instructions: 'You analyze documents',
      tools: ['read_file']
    });
    
    await agent.chat('Summarize the contents of report.pdf');
    // Agent reads and summarizes the file
  2. With Configuration
    typescript
    const agent = new Agent({
      tools: ['read_file', 'write_file']
    });
    
    await agent.chat('Create a summary.txt with key points');
    // Agent creates the file

User Interaction Flow

Diagram
FileSystemAgentUserFileSystemAgentUser“Read config.json”read_file(‘config.json’)File contents“The config contains…”

Configuration Levels

typescript
// Level 1: Array - Enable file tools
const agent = new Agent({
  tools: ['read_file', 'write_file']
});

// Level 2: String - Allow directory access
const agent = new Agent({
  tools: ['read_file'],
  allowedPaths: './docs/'
});

// Level 3: Dict - Full control
const agent = new Agent({
  files: {
    read: true,
    write: true,
    delete: false,
    allowedPaths: ['./docs/', './output/'],
    maxFileSize: '10MB'
  }
});

File Tools

ToolDescription
read_fileRead file contents
write_fileCreate or update files
list_filesList directory contents
delete_fileRemove files (use carefully)

API Reference

Tools Module

Tools including file operations


Best Practices

Limit allowed paths

Restrict file access to specific directories.

Disable delete by default

Only enable delete_file when absolutely needed.

Use approval for writes

Combine with approval for production safety.


Related