Skip to content

gunnarnordqvist/opencode-context-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenCode Context Filter Proxy

License: MIT Python 3.6+

I implemented a Context Filter Proxy—a transparent HTTP proxy that automatically filters repository context for small language models (llama3.2:1b, qwen2.5:1.5b), reducing token overhead by 99% (45,000 → 200 tokens) and achieving 8× faster inference (8s → 1s). Installation requires running ./scripts/install.sh and updating OpenCode's baseURL to port 11435; the proxy then launches automatically, delivering ~112 seconds aggregate time savings across 16 subagents per invocation cycle without modifying OpenCode.

Quick Start

# Clone the repository
git clone https://github.com/gunnarnordqvist/opencode-context-filter.git
cd opencode-context-filter

# Install
./scripts/install.sh

# Reload shell
source ~/.bashrc

# Update OpenCode configuration (~/.config/opencode/opencode.json)
# Change: "baseURL": "http://localhost:11434/v1"
# To:     "baseURL": "http://localhost:11435/v1"

# Run OpenCode - proxy starts automatically!
opencode

Performance

Metric Before After Improvement
Context tokens ~45,000 ~200 99% reduction
Inference time ~8s ~1s 8× faster
Memory usage High Low Significant
Time saved (16 subagents) - ~112s Per cycle

Features

  • Transparent: No OpenCode modifications required
  • Automatic: Starts with OpenCode via wrapper script
  • Smart: Filters only small models (llama3.2:1b, qwen2.5:1.5b)
  • Fast: <50ms additional latency
  • Efficient: Removes 99% of unnecessary context
  • Portable: Python standard library only, no dependencies

What Gets Filtered

For small models, the proxy removes:

  • <project> section - Full repository tree (~10,000-50,000 tokens)
  • <env> section - Detailed environment information
  • AGENTS.md - Instruction files (5,000+ lines)
  • ❌ Custom instructions - Project-specific rules

Retains only:

  • ✅ Minimal <env> - Working directory, platform, date
  • ✅ Agent prompt - Specific instructions for the subagent

Documentation

How It Works

flowchart TD
    A[User: opencode] --> B[opencode-wrapper.sh]
    B -->|Check proxy status| C{Proxy running?}
    C -->|Yes| D[Launch OpenCode]
    C -->|No| E[Start proxy]
    E --> D
    D --> F[OpenCode sends request]
    F --> G[http://localhost:11435/v1<br/>Proxy]
    G --> H[Filter context for llama3.2:1b]
    H --> I[http://localhost:11434<br/>Ollama]
    I --> J[Generate response]
    J --> K[Response → OpenCode]
Loading

Testing

# Basic functionality test
python3 tests/test_basic.py

# Realistic context test  
python3 tests/test_realistic.py

# Extreme context test
python3 tests/test_extreme.py

Monitoring

# View real-time proxy logs
tail -f /tmp/ollama_context_filter.log

# See filtering activity
tail -f /tmp/ollama_context_filter.log | grep FILTER

Example output:

[FILTER] Model: llama3.2:1b, Original tokens: ~483, Filtered tokens: ~176

Configuration

Add More Models

Edit src/ollama_context_filter_proxy.py:

SMALL_MODELS = [
    "llama3.2:1b",
    "llama3.2-1b",
    "qwen2.5:1.5b",
    "qwen2.5-1.5b",
    "phi-2",           # Add your model
    "tinyllama",       # Add another
]

Change Ports

Edit scripts/opencode-wrapper.sh:

PROXY_PORT=11435  # Modify as required

Update OpenCode configuration accordingly.

Requirements

  • Python 3.6+
  • OpenCode (any version)
  • Ollama with llama3.2:1b or qwen2.5:1.5b

Licence

MIT Licence - See LICENSE file

Contributing

Contributions welcome! Please see CONTRIBUTING.md for guidelines.

Support

Related Projects

  • OpenCode - AI coding agent for the terminal
  • Ollama - Run large language models locally

Acknowledgements

This proxy serves as a temporary workaround pending native context control implementation in OpenCode.


Status: Production-ready
Tested on: OpenCode 1.x with llama3.2:1b and qwen2.5:1.5b
Performance verified: 8× inference improvement, 99% context reduction

About

Transparent HTTP proxy that automatically filters repository context for small language models in OpenCode, achieving 99% token reduction and 8× faster inference

Topics

Resources

License

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors