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.
# 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| 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 |
- ✅ 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
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
- Setup Guide - Complete installation and configuration
- Usage Guide - How to use the proxy
- Troubleshooting - Common issues and solutions
- Technical Details - Architecture and implementation
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]
# Basic functionality test
python3 tests/test_basic.py
# Realistic context test
python3 tests/test_realistic.py
# Extreme context test
python3 tests/test_extreme.py# View real-time proxy logs
tail -f /tmp/ollama_context_filter.log
# See filtering activity
tail -f /tmp/ollama_context_filter.log | grep FILTERExample output:
[FILTER] Model: llama3.2:1b, Original tokens: ~483, Filtered tokens: ~176
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
]Edit scripts/opencode-wrapper.sh:
PROXY_PORT=11435 # Modify as requiredUpdate OpenCode configuration accordingly.
- Python 3.6+
- OpenCode (any version)
- Ollama with llama3.2:1b or qwen2.5:1.5b
MIT Licence - See LICENSE file
Contributions welcome! Please see CONTRIBUTING.md for guidelines.
- GitHub Issues: Report bugs or request features
- Upstream Feature Request: OpenCode #4096
- Proxy Logs:
/tmp/ollama_context_filter.log
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