AI-Assisted Coding
Integrate OpenClaw AI into your development workflow for automated code review, test generation, documentation, and security analysis.
Prerequisites
- OpenClaw v1.0.0+ installed
- An API key for GPT-4o, Claude, or a local coding model (e.g.
codellamavia Ollama) - A code repository or file to review
- Optional: GitHub CLI (
gh) for pull request automation
Automated Code Review
# Review a single file
openclaw run "Review this Python file for bugs, security issues, and style" \
--file src/api/auth.py
# Review staged git changes
git diff --staged | openclaw run "Review these code changes for issues"
# Review a pull request diff
git diff main...feature-branch | openclaw run "Code review: identify bugs and improvements"Test Generation
# Generate unit tests
openclaw run "Write pytest unit tests with 100% branch coverage for this function" \
--file src/utils/parser.py \
--output tests/test_parser.py
# Generate integration tests
openclaw run "Write integration tests for the REST API endpoints" \
--file src/api/routes.pyAuto Documentation
# Add docstrings to all functions
openclaw run "Add Google-style docstrings to all functions and classes" \
--file src/models/user.py \
--output src/models/user_documented.py
# Generate README
openclaw run "Generate a comprehensive README.md for this project" \
--context ./src \
--output README.mdRefactoring Assistance
# Modernize legacy code
openclaw run "Refactor this code to use modern Python 3.11 features and type hints" \
--file legacy/old_script.py
# Extract functions
openclaw run "Identify and extract duplicate code into reusable functions" \
--file src/main.pySecurity Analysis
# Scan for vulnerabilities
openclaw run "Perform a security audit: check for SQL injection, XSS, SSRF, and insecure dependencies" \
--context ./src \
--provider claude # Claude's large context handles entire codebases
# Check for secrets in code
openclaw run "Scan this codebase for accidentally committed API keys, passwords, or secrets" \
--context .CI/CD Integration
# .github/workflows/ai-review.yml
name: AI Code Review
on: [pull_request]
jobs:
ai-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install OpenClaw
run: pip install openclaw
- name: AI Code Review
run: |
git diff ${{ github.base_ref }}..HEAD | \
openclaw run "Review: identify bugs, security issues, and suggest improvements" \
--output ai-review.md
- name: Post Review Comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const review = fs.readFileSync('ai-review.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: review
})
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}AI-Assisted Debugging
Paste error messages, stack traces, or failing tests and let OpenClaw diagnose the root cause:
# Debug a stack trace
cat traceback.txt | openclaw run "Identify the root cause of this Python traceback and suggest the fix"
# Find why tests fail
openclaw run "Explain why this Python test fails and fix the implementation" \
--file test_auth.py \
--context-file auth.py
# Debug runtime behavior
openclaw run "Why does this PostgreSQL query take 45 seconds? Suggest optimizations." \
--file slow-query.sql \
--context-file query-plan.txtCode Generation
# Generate boilerplate from a description
openclaw run "Generate a FastAPI endpoint: POST /api/users/register. Validates email + password, hashes password with bcrypt, saves to PostgreSQL via SQLAlchemy, returns 201 on success." \
--format python \
--output register_endpoint.py
# Generate a configuration file
openclaw run "Create a Docker Compose file for: FastAPI backend, PostgreSQL 14, Redis, and Nginx reverse proxy. Include health checks and environment variables." \
--format yaml \
--output docker-compose.yml
# Convert between languages
openclaw run "Convert this JavaScript function to TypeScript with proper types" \
--file utils.js \
--format typescriptTry It Yourself
Run these coding assistant examples on any real codebase or create test files to experiment:
1 — Instant code review
# Review any Python file in your current project
openclaw run "Review this Python file for bugs, security issues, and code quality problems. Give me a prioritised list with line numbers." --file your_script.pyExpected output: A numbered list of issues, each with the line number, issue type (Bug / Security / Style), severity, and a suggested fix.
2 — Auto-generate tests
# Generate pytest tests for a module
openclaw run "Write comprehensive pytest unit tests for this module. Include edge cases, happy paths, and error conditions. Use fixtures where appropriate." --file src/user_service.py --output tests/test_user_service.py
# Immediately run the generated tests
python -m pytest tests/test_user_service.py -v3 — Generate docstrings for an entire module
# Add Google-style docstrings to all undocumented functions
openclaw run "Add Google-style docstrings to every function and class in this file that currently has no docstring. Return the complete updated file." --file src/utils.py --output src/utils_documented.pyFor best results with code review, use
--model gpt-4o or claude-3-5-sonnet. These models understand code context better than smaller models.What's Next
- Task Automation — automate repetitive development tasks
- AI Agent Best Practices — security and code quality best practices
- Advanced CLI Usage — power user CLI patterns for developers
- Tutorials — step-by-step: build a personal AI coding assistant