Skip to main content

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.

PyPI License: MIT Platform

๐Ÿ”— 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


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

context_bus-0.2.1.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

context_bus-0.2.1-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

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

Hashes for context_bus-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a6f31c28786f1414269a9a53c6c6c3b902d0d6c28c99ef9e9a04dab6150b7f95
MD5 44e2ecff2cf51a9c6dc487476a3137ef
BLAKE2b-256 b67c8c87e769b4e891ed74230f2df5147e1700d826154c284042e95342161922

See more details on using hashes here.

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

Hashes for context_bus-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f6efa7abb8c3065e134ec44db65cecf2e8b321fcbd033900a889b53bd894bfe1
MD5 2f1addd31affc2688942ae4691b9b909
BLAKE2b-256 e464cfde3d426ac2396c1ec5188fb44b7b8877ca1b5d76c5cc0b91b2746b03dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page