Skip to main content

Optimization Commands

Commands for optimizing individual functions or entire codebases.

Optimize a Single Function

Target a specific function in a file for optimization.
codeflash --file <path/to/file.py> --function <function_name>
# Basic optimization (creates PR)
codeflash --file src/utils.py --function calculate_metrics

# Local optimization only (no PR)
codeflash --file src/utils.py --function calculate_metrics --no-pr

# With verbose output
codeflash --file src/utils.py --function calculate_metrics --no-pr --verbose
Important: The file must be within your configured module-root directory. Files outside module-root will be ignored with β€œFunctions outside module-root” message.

Optimize All Functions

Optimize all functions in your entire codebase or a specific directory.
# Optimize entire codebase
codeflash --all

# Optimize specific directory
codeflash --all src/core/
# Optimize all (creates PRs)
codeflash --all

# Optimize all locally (no PRs)
codeflash --all --no-pr

# Optimize specific directory
codeflash --all src/algorithms/ --no-pr

# Skip draft PRs in CI
codeflash --all --no-draft
When using --all, Codeflash will:
  • Discover all optimizable functions in your codebase
  • Create separate PRs for each function (or update locally with --no-pr)
  • Process functions in batches to avoid overwhelming your repository

Benchmark Mode

Optimize code based on performance benchmarks using pytest-benchmark format.
codeflash --file <file.py> --benchmark --benchmarks-root <path>
# With benchmarks-root flag
codeflash --file src/core.py --benchmark --benchmarks-root tests/benchmarks --no-pr

# If benchmarks-root is in pyproject.toml
codeflash --file src/core.py --benchmark --no-pr
The --benchmarks-root directory must exist and be configured either via pyproject.toml or the command-line flag.

Next Steps