Automatic model switching for AI coding agents with full context preservation
Project description
Context Bus v3
Seamless multi-model orchestration for OpenClaw agents.
Automatically switch between Claude Opus, Codex, and Gemini when you hit usage limits โ without losing context. Full context preservation with atomic writes, handoff expiry, and automatic switch-back.
๐ GitHub: https://github.com/rajathkm/context-bus
๐ฆ Install:
pip install context-bus && context-bus init
โ ๏ธ Safe Install โ Your Files Are Never Overwritten
| File | If It Exists | If It Doesn't Exist |
|---|---|---|
AGENTS.md |
โ Left untouched | Created with template |
MEMORY.md |
โ Left untouched | Created with template |
HEARTBEAT.md |
โ Only adds Context Bus section | Created with rules |
config.yaml |
โ Left untouched | Created with defaults |
What's New in v3
| Feature | Description |
|---|---|
| Handoff Expiry | 5-minute validity โ no stale handoffs |
| Switch-Back | Auto-returns to Opus when limits reset (<50%) |
| Atomic Writes | Merge-based updates, no data loss |
| Model History | Tracks all switches for multi-model flows |
| Offline Detection | Skips switch if agent inactive >30 min |
| Log Rotation | Auto-compresses logs at 10MB |
Quick Start (OpenClaw/Clawdbot)
1. Install
pip install context-bus
context-bus init
Or tell your Clawdbot:
"Install Context Bus for automatic model switching"
2. That's It
The installer automatically:
- โ
Creates
.context-bus/with handoff.json (v3 schema) - โ Adds monitoring rules to HEARTBEAT.md
- โ Sets up background monitor (every 5 min)
- โ
Creates config at
~/.config/context-bus/config.yaml
How It Works
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ YOUR CLAWDBOT โ
โ โ
โ Heartbeat fires โ
โ โ โ
โ session_status โ "Usage: 87%" โ
โ โ โ
โ Updates .context-bus/handoff.json โ
โ โ โ
โ handoff_ready = true, expires in 5 min โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ BACKGROUND MONITOR (every 5 min) โ
โ โ
โ Reads handoff.json โ usage = 96% โ
โ Validates: ready? not expired? checksum OK? โ
โ โ โ
โ 96% >= 95% โ SWITCH โ
โ Updates model.current = "codex" โ
โ Adds to model.history โ
โ Notifies: "๐ Opus โ Codex (96%)" โ
โ โ
โ [Later: usage drops to 40%] โ
โ 40% < 50% AND task idle โ SWITCH BACK โ
โ Notifies: "๐ Codex โ Opus (limits reset)" โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
File Structure
WORKSPACE/
โโโ .context-bus/
โ โโโ handoff.json โ v3 schema with expiry
โ โโโ rolling-summary.md โ Session summary (YAML frontmatter)
โ โโโ history.jsonl โ Event log (rotated at 10MB)
โ โโโ usage-monitor.sh โ Background monitor
โ โโโ handoff-utils.sh โ Atomic write utilities
โ
โโโ AGENTS.md โ References .context-bus/
โโโ MEMORY.md โ Long-term memory
โโโ HEARTBEAT.md โ Context Bus rules
~/.config/context-bus/
โโโ config.yaml โ Thresholds, notifications
Handoff.json v3 Schema
{
"schema_version": 3,
"sequence": 47,
"timestamp": "2026-02-01T15:15:00+05:30",
"handoff_ready": true,
"handoff_expires": "2026-02-01T15:20:00+05:30",
"checksum": "sha256:abc123...",
"model": {
"current": "codex",
"usage_percent": 96,
"history": [
{"model": "opus", "until": "...", "reason": "auto_threshold"}
]
},
"task": {
"description": "Building authentication",
"status": "in_progress"
},
"context": {
"recent_actions": ["Created auth.ts", "Added tests"],
"decisions": ["Using JWT with 1h expiry"],
"next_steps": ["Add refresh logic"]
}
}
CLI Commands
# Initialize in workspace
context-bus init
# Check status
context-bus status
# Update usage (from heartbeat)
context-bus update 87
# Manual switch
context-bus switch codex --reason "testing"
# View config
context-bus config --show
Configuration
Edit ~/.config/context-bus/config.yaml:
workspace: ~/clawd
models:
primary: opus
secondary: codex
tertiary: gemini
thresholds:
switch_to_secondary: 95 # Switch at 95%
switch_back: 50 # Return at <50%
proactive_summary: 80 # Generate summary at 80%
safety:
max_handoff_age_seconds: 300
offline_abort_minutes: 30
notifications:
channel: telegram
telegram:
chat_id: "YOUR_CHAT_ID"
Guarantees
| Guarantee | How |
|---|---|
| No stale handoff | 5-min expiry + age validation |
| No data loss | Merge-based atomic writes |
| No missed switches | 5-min monitor interval |
| No stuck on fallback | Auto switch-back at <50% |
| No offline mistakes | Abort if inactive >30 min |
| No log overflow | Rotate at 10MB |
Platform Support
| Platform | Monitor | Status |
|---|---|---|
| macOS | launchd | โ Full support |
| Linux | cron | โ Full support |
| Windows | Task Scheduler | โ Supported |
| WSL | cron | โ Works |
Troubleshooting
Monitor not running
# macOS
launchctl list | grep contextbus
# View logs
tail -20 ~/.context-bus/monitor.log
Handoff expired
The agent needs to update handoff.json every 5 minutes. Ensure HEARTBEAT.md rules are running.
Switch-back not happening
Check task status โ switch-back only happens when task.status is "idle" or "completed".
Links
- PyPI: https://pypi.org/project/context-bus/
- GitHub: https://github.com/rajathkm/context-bus
- Issues: https://github.com/rajathkm/context-bus/issues
License
MIT License - see LICENSE
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_bus-0.2.1.tar.gz.
File metadata
- Download URL: context_bus-0.2.1.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6f31c28786f1414269a9a53c6c6c3b902d0d6c28c99ef9e9a04dab6150b7f95
|
|
| MD5 |
44e2ecff2cf51a9c6dc487476a3137ef
|
|
| BLAKE2b-256 |
b67c8c87e769b4e891ed74230f2df5147e1700d826154c284042e95342161922
|
File details
Details for the file context_bus-0.2.1-py3-none-any.whl.
File metadata
- Download URL: context_bus-0.2.1-py3-none-any.whl
- Upload date:
- Size: 32.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6efa7abb8c3065e134ec44db65cecf2e8b321fcbd033900a889b53bd894bfe1
|
|
| MD5 |
2f1addd31affc2688942ae4691b9b909
|
|
| BLAKE2b-256 |
e464cfde3d426ac2396c1ec5188fb44b7b8877ca1b5d76c5cc0b91b2746b03dd
|