Skip to main content
PraisonAI Code provides AI agents with powerful tools to read, write, and modify code files with surgical precision. Inspired by Kilo Code’s architecture, it uses a SEARCH/REPLACE diff strategy with fuzzy matching for reliable code modifications. The user asks the agent to edit project files; read, write, and SEARCH/REPLACE diffs apply changes safely.

How It Works

Quick Start

1

Set the workspace

When you launch praisonai code from the terminal, the CLI resolves the workspace from --workspace / PRAISONAI_WORKSPACE / the resumed session’s directory and calls set_workspace for you (see Workspace directory). Call set_workspace yourself only when embedding the code tools in your own Python script.
2

Read and patch a file

Available Tools

Using with Agents

The CODE_TOOLS list contains all tools ready for use with PraisonAI agents:

Tool Reference

code_read_file

Read file contents with optional line ranges and line number annotations.
Output format:

code_write_file

Create new files or completely replace existing files.
For partial modifications, use code_apply_diff instead of overwriting the entire file.

code_list_files

List files and directories with optional filtering.
Output format:

code_apply_diff

Apply precise, surgical modifications using SEARCH/REPLACE diffs.

Diff Format

The diff format uses clear markers:
  • :start_line:N - Optional line number hint for faster matching
  • ------- - Separator after line hints
  • ======= - Divider between search and replace content
  • Multiple blocks can be included in a single diff

Multiple Changes

Apply multiple changes in one operation:

code_search_replace

Simple search and replace for straightforward text substitutions.

code_execute_command

Execute shell commands safely within the workspace.

Fuzzy Matching

The diff strategy includes fuzzy matching using Levenshtein distance, which helps when:
  • Code has been slightly modified since you read it
  • There are minor whitespace differences
  • Line numbers have shifted due to other changes

Indentation Preservation

When applying diffs, indentation is automatically preserved:

Security Features

Workspace Isolation

All file operations are restricted to the configured workspace:

Gitignore Support

The code_list_files tool respects .gitignore patterns by default:

Safe Command Execution

Commands are executed with safety checks:

Crash-Safe Writes

code_write_file, code_search_replace, and code_apply_diff write files atomically. Each write goes to a temp file in the target’s directory, is flushed and fsync’d, then swapped over the target with os.replace. If the agent is cancelled, times out, is SIGTERM’d, or the disk fills up mid-write, the original file is untouched. Nothing changes for the caller when a write succeeds — same return value, same file. On failure, the temp file (.tmp_<hex> next to the target) is removed before the error propagates.
Upgrade note (PraisonAI PR #6205): before this release, these three tools used open(path, 'w'), which truncates the target immediately. A cancelled or timed-out tool call could leave your file empty or half-written and lose the original content. Upgrade to get the crash-safe write guarantee.

Low-Level API

For advanced use cases, you can use the low-level functions directly:

Helper Functions

Creating Diff Blocks Programmatically

Best Practices

Read the file first to get the exact content for your SEARCH block:
Include :start_line:N for faster and more accurate matching:
Group related changes in a single diff, but keep unrelated changes separate for easier debugging.
code_write_file, code_search_replace, and code_apply_diff write atomically — if the agent is cancelled, times out, or is killed mid-write, the original file is untouched. You don’t need a manual backup for crash safety (you may still want one for content-level rollback).
After applying a diff, read the file again or run tests to verify:

Complete Example

Here’s a complete workflow showing an agent that adds error handling to a function:

Code Interpreter

Execute Python code dynamically

Tools

Learn about custom tools

Code UI

Web interface for code interaction

Agents

Learn about PraisonAI agents

MultiEdit

Apply many edits to one file atomically and lock-safely — shares the same crash-safe write helper