Codeflash CLI Reference
This guide covers all Codeflash CLI commands with practical examples you can run directly in your terminal.Prerequisites - Ensure Codeflash is installed in your Python environment
and you have a configured
pyproject.toml in your project.Quick Start
- Linux/macOS
- Windows
Common Workflows
1. First-Time Setup
1
Install Codeflash
2
Initialize Project
3
Verify Setup
4
Run First Optimization
2. Optimize a Workflow
1
Trace Your Script
2
Review Optimizations
Check the generated PR or local changes for optimization suggestions.
3. CI/CD Integration
1
Set Up GitHub Actions
2
Merge the Workflow PR
Review and merge the generated GitHub Actions workflow.
3
Automatic Optimization
Codeflash will now optimize code in every PR automatically!
Help & Version
Complete Flag Reference
Main Command Flags
| Flag | Type | Description |
|---|---|---|
--file | PATH | Optimize only this file |
--function | NAME | Optimize only this function (requires --file) |
--all | [PATH] | Optimize all functions. Optional path to start from |
--replay-test | PATH | Path to replay test file(s) |
--benchmark | flag | Enable benchmark mode |
--no-pr | flag | Don’t create PR, update locally |
--no-gen-tests | flag | Don’t generate tests |
--no-draft | flag | Skip draft PRs |
--worktree | flag | Use git worktree |
--staging-review | flag | Upload to staging |
--verbose / -v | flag | Verbose debug output |
--verify-setup | flag | Run setup verification |
--version | flag | Show version |
Configuration Override Flags
| Flag | Type | Description |
|---|---|---|
--config-file | PATH | Path to pyproject.toml |
--module-root | PATH | Python module root directory |
--tests-root | PATH | Tests directory |
--benchmarks-root | PATH | Benchmarks directory |
Optimize Subcommand Flags
| Flag | Type | Description |
|---|---|---|
--output | PATH | Trace file output path (default: codeflash.trace) |
--timeout | INT | Maximum trace time in seconds |
--max-function-count | INT | Max times to trace a function (default: 100) |
--config-file-path | PATH | Path to pyproject.toml |
--trace-only | flag | Only trace, don’t optimize |
Troubleshooting
'Functions outside module-root' Error
'Functions outside module-root' Error
Problem: Function not found because file is outside
module-root.Solution: Ensure your file is within the module-root directory specified in pyproject.toml.'benchmarks-root must be specified' Error
'benchmarks-root must be specified' Error
Problem: Using
--benchmark without specifying benchmarks directory.Solution: Either add benchmarks-root to pyproject.toml or use the flag:Replay Test File Not Found
Replay Test File Not Found
Problem: Replay test filename doesn’t match expected path.Solution: Replay tests include the module path in their name. Check the actual filename:
GitHub App Required
GitHub App Required
Problem: PR creation fails due to missing GitHub App.Solution: Install the Codeflash GitHub App or use
--no-pr for local optimization:Core Commands
codeflash init
Initialize Codeflash for your Python project. This creates the configuration in pyproject.toml.
codeflash init-actions
Set up GitHub Actions workflow for continuous optimization on every pull request.
- Creates a workflow file in
.github/workflows/ - Opens a PR with the workflow configuration
- Requires the Codeflash GitHub App to be installed
codeflash vscode-install
Install the Codeflash extension for VS Code, Cursor, or Windsurf.
codeflash --verify-setup
Verify your Codeflash installation by running a sample optimization.
This command creates a temporary file, runs a sample optimization, and cleans
up afterward. It takes about 3 minutes to complete.
Function Optimization
Optimize a Single Function
Target a specific function in a file for optimization.Complete Examples
Complete Examples
- Linux/macOS
- Windows
Optimize All Functions
Optimize all functions in your entire codebase or a specific directory.Complete Examples
Complete Examples
- Linux/macOS
- Windows
Trace & Optimize Workflows
codeflash optimize
Trace a Python script’s execution and optimize functions based on real-world usage.
Complete Examples
Complete Examples
- Linux/macOS
- Windows
Trace with pytest
Optimize functions called during pytest test execution.- Linux/macOS
- Windows
Trace Only (Generate Replay Tests)
Create trace files and replay tests without running optimization.- Linux/macOS
- Windows
Replay test naming: Files are named based on the traced script path. For
src/app.py, the replay test will be named like
test_srcapp_py__replay_test_0.py.Benchmark Mode
Optimize code based on performance benchmarks using pytest-benchmark format.Complete Examples
Complete Examples
- Linux/macOS
- Windows
Configuration Flags
Override configuration settings from the command line.| Flag | Description |
|---|---|
--config-file | Path to pyproject.toml with Codeflash config |
--module-root | Path to Python module to optimize |
--tests-root | Path to test directory |
--benchmarks-root | Path to benchmarks directory |
Complete Examples
Complete Examples
- Linux/macOS
- Windows
Behavior Flags
| Flag | Description |
|---|---|
--no-pr | Run locally without creating a pull request |
--no-gen-tests | Use only existing tests, skip test generation |
--no-draft | Skip optimization for draft PRs (CI mode) |
--worktree | Use git worktree for isolated optimization |
--staging-review | Upload optimizations to staging for review |
--verbose / -v | Enable verbose debug logging |
Complete Examples
Complete Examples