How It Works
Quick Start
1
Set the workspace
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
TheCODE_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.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.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
Thecode_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.
Low-Level API
For advanced use cases, you can use the low-level functions directly:Helper Functions
Creating Diff Blocks Programmatically
Best Practices
Always read before modifying
Always read before modifying
Read the file first to get the exact content for your SEARCH block:
Use line number hints
Use line number hints
Include
:start_line:N for faster and more accurate matching:Make atomic changes
Make atomic changes
Group related changes in a single diff, but keep unrelated changes separate for easier debugging.
Trust the tools on cancellation
Trust the tools on cancellation
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).Verify changes
Verify changes
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:Related
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

