Skip to main content

Automatic model switching for AI coding agents with full context preservation

Project description

Context Bus

Automatic model switching for AI coding agents with full context preservation.

Never lose context when switching between Claude Opus, Codex, Gemini CLI, or local models. Context Bus automatically switches when you hit usage limits and switches back when limits reset โ€” all while preserving your conversation context.

License: MIT Platform

Features

  • ๐Ÿ”„ Auto-switch at configurable usage threshold (default: 95%)
  • ๐Ÿ”™ Auto-return to primary model when limits reset
  • ๐Ÿ“ฆ Context preservation via structured handoff files
  • ๐Ÿ“ฑ Notifications via Telegram, Discord, or Slack
  • โš™๏ธ Configurable primary/secondary/tertiary models
  • ๐Ÿ”’ Safe switching โ€” won't interrupt active tasks
  • ๐Ÿค– OpenClaw/Clawdbot integration โ€” works out of the box

What Context Bus Does

Context Bus provides two key capabilities:

1. Shared Context Layer (like Agent Sync, but for context)

All models read from the same files:

  • AGENTS.md - Project context, constraints, current task
  • MEMORY.md - Long-term memory, decisions, preferences
  • handoff.json - Structured state for machine parsing

Edit once โ†’ all models see the same context. No re-explaining when you switch models.

2. Automatic Model Switching

When you hit usage limits:

  • Auto-generates context summary
  • Switches to fallback model
  • Preserves full context
  • Notifies you
  • Auto-returns when limits reset

Comparison with Agent Rules Sync

Feature Agent Rules Sync Context Bus
Syncs rules โœ… Yes โŒ No
Syncs context โŒ No โœ… Yes
Model switching โŒ No โœ… Yes
Handoff protocol โŒ No โœ… Yes
Usage monitoring โŒ No โœ… Yes

Use both together! Agent Rules Sync for consistent rules + Context Bus for shared context and model switching.


Installation

Works on macOS, Linux, and Windows (native + WSL).

Via pip (Recommended)

pip install context-bus
context-bus init

Via curl (One-liner)

curl -fsSL https://raw.githubusercontent.com/rajathkm/context-bus/main/install.sh | bash

Via Clawdbot/OpenClaw

Just ask your agent:

"Install Context Bus for automatic model switching"

Manual Install

git clone https://github.com/rajathkm/context-bus.git
cd context-bus
./install.sh

What Happens During Installation

  1. โœ… Scripts installed to ~/.context-bus/
  2. โœ… Config template created at ~/.config/context-bus/config.yaml
  3. โœ… Initial handoff.json and rolling-summary.md created
  4. โœ… PATH updated (optional)

Quick Start

1. Configure Your Models

Edit ~/.config/context-bus/config.yaml:

models:
  primary: opus          # Your main model (Claude Opus 4.5)
  secondary: codex       # Fallback model (OpenAI Codex)
  tertiary: gemini       # Optional third model (Gemini CLI)

thresholds:
  switch_to_secondary: 95    # Switch at 95% usage
  switch_back: 50            # Switch back when usage drops (indicates reset)

2. Set Up Notifications

notifications:
  enabled: true
  channel: telegram
  telegram:
    chat_id: "YOUR_CHAT_ID"

3. Add to Your Agent

For OpenClaw/Clawdbot users:

The installer automatically adds Context Bus rules to your HEARTBEAT.md. Your agent will:

  • Check usage during heartbeats
  • Auto-switch at threshold
  • Notify you on Telegram
  • Switch back when limits reset

For other agents (Claude Code, Cursor, etc.):

Add to your agent's system instructions:

## Context Bus Integration
Before starting work, read ~/.context-bus/handoff.json for current state.
After significant actions, update the handoff file.

OpenClaw/Clawdbot Integration

Context Bus is designed to work seamlessly with OpenClaw (Clawdbot).

Automatic Installation

Your Clawdbot can install Context Bus itself:

You: "Install the Context Bus skill for automatic model switching"
Clawdbot: *installs and configures Context Bus*

Manual Integration

Add to your HEARTBEAT.md:

## Context Bus Auto-Switch

1. Check usage via `session_status`
2. If usage >= 95% AND model is "opus":
   - Run handoff script
   - Switch to Codex
   - Notify on Telegram
3. If usage < 50% AND model is "codex" AND tasks are idle:
   - Switch back to Opus
   - Notify on Telegram

How It Works with Clawdbot

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     YOUR CLAWDBOT                           โ”‚
โ”‚                                                             โ”‚
โ”‚  HEARTBEAT.md โ†โ”€โ”€ Context Bus Rules                        โ”‚
โ”‚       โ†“                                                     โ”‚
โ”‚  session_status โ†’ Check Usage                               โ”‚
โ”‚       โ†“                                                     โ”‚
โ”‚  95%+ reached? โ†’ Run context-handoff.sh                     โ”‚
โ”‚       โ†“                                                     โ”‚
โ”‚  handoff.json + rolling-summary.md generated                โ”‚
โ”‚       โ†“                                                     โ”‚
โ”‚  Telegram notification sent                                 โ”‚
โ”‚       โ†“                                                     โ”‚
โ”‚  Model switched (Opus โ†’ Codex)                             โ”‚
โ”‚       โ†“                                                     โ”‚
โ”‚  Context preserved โœ“                                        โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Configuration Reference

Full Config Example

# ~/.config/context-bus/config.yaml

# =============================================================================
# MODEL CONFIGURATION
# =============================================================================
models:
  primary:
    name: opus
    cli: claude
    usage_check: session_status
    
  secondary:
    name: codex
    cli: codex
    usage_check: none
    
  tertiary:
    name: gemini
    cli: gemini
    enabled: false
    
  local:
    name: llama3.1:8b
    provider: ollama
    endpoint: http://localhost:11434
    enabled: false

# =============================================================================
# SWITCHING RULES
# =============================================================================
thresholds:
  switch_to_secondary: 95
  switch_back: 50
  min_switch_interval: 300    # Seconds between switches

# =============================================================================
# NOTIFICATIONS
# =============================================================================
notifications:
  enabled: true
  channel: telegram           # telegram | discord | slack | none
  
  telegram:
    chat_id: ""
    
  discord:
    webhook_url: ""
    
  slack:
    webhook_url: ""
    
  events:
    on_switch: true
    on_return: true
    on_error: true
    on_threshold_warning: true

# =============================================================================
# SAFETY RULES
# =============================================================================
safety:
  check_tasks_before_return: true
  check_subagents: true
  wait_if_busy: true
  max_wait_heartbeats: 10

# =============================================================================
# PATHS
# =============================================================================
paths:
  context_dir: ~/.context-bus
  workspace_files:
    - AGENTS.md
    - MEMORY.md
    - memory/*.md

CLI Commands

# Check current status
context-bus status

# Manual model switching
context-bus switch codex        # Switch to secondary
context-bus switch opus         # Switch to primary
context-bus switch --back       # Return to primary (if safe)

# Generate handoff manually
context-bus handoff

# Refresh rolling summary
context-bus summary

# View/edit config
context-bus config --show
context-bus config --edit

Context Preservation

The Handoff System

When switching models, Context Bus creates a complete context bundle:

~/.context-bus/
โ”œโ”€โ”€ handoff.json           # Structured state (machine-readable)
โ”œโ”€โ”€ rolling-summary.md     # Human-readable summary (<400 tokens)
โ”œโ”€โ”€ relevant-context.md    # Semantic search results (via qmd)
โ””โ”€โ”€ handoff-prompt.md      # Ready-to-use prompt for new model

handoff.json Schema

{
  "version": 1,
  "timestamp": "2026-02-01T14:00:00Z",
  "task": {
    "id": "feature-123",
    "description": "Implementing user authentication",
    "status": "in_progress",
    "complexity": "high"
  },
  "model": {
    "current": "codex",
    "previous": "opus",
    "switch_reason": "usage_threshold",
    "usage_percent": 96
  },
  "context_refs": [
    "AGENTS.md",
    "src/auth/oauth.ts"
  ],
  "recent_actions": [
    {"action": "edit", "file": "auth.ts", "summary": "Added OAuth flow"}
  ],
  "blockers": [],
  "next_steps": ["Add token refresh", "Write tests"],
  "notes": {
    "opus": "Using JWT with 1h expiry"
  }
}

rolling-summary.md Example

# Session Summary
Last updated: 2026-02-01 14:00 IST

## Current Focus
Implementing OAuth authentication flow for the API.

## Recent Progress
- Created OAuth client configuration
- Added token exchange endpoint
- Implemented session management

## Key Decisions
- Using JWT tokens with 1-hour expiry
- Refresh tokens stored in httpOnly cookies

## Open Questions
- None currently

Supported Models

Model CLI Command Install Usage Check
Claude Opus 4.5 claude Claude Code session_status
OpenAI Codex codex npm install -g @openai/codex None
Gemini CLI gemini Gemini CLI API
Local (Ollama) ollama brew install ollama None

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        CONTEXT BUS                          โ”‚
โ”‚                                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚   Config    โ”‚    โ”‚   Monitor   โ”‚    โ”‚   Switch    โ”‚     โ”‚
โ”‚  โ”‚             โ”‚    โ”‚             โ”‚    โ”‚             โ”‚     โ”‚
โ”‚  โ”‚ config.yaml โ”‚ โ†’  โ”‚ Check usage โ”‚ โ†’  โ”‚  Handoff +  โ”‚     โ”‚
โ”‚  โ”‚ thresholds  โ”‚    โ”‚ via status  โ”‚    โ”‚  Notify     โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                              โ”‚              โ”‚
โ”‚                                              โ†“              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚                   CONTEXT LAYER                      โ”‚   โ”‚
โ”‚  โ”‚                                                      โ”‚   โ”‚
โ”‚  โ”‚  handoff.json    rolling-summary.md    qmd search   โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                                              โ”‚              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                               โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         MODELS                              โ”‚
โ”‚                                                             โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚   โ”‚  Opus   โ”‚    โ”‚  Codex  โ”‚    โ”‚ Gemini  โ”‚   โ”‚  Local  โ”‚  โ”‚
โ”‚   โ”‚ Primary โ”‚    โ”‚Secondaryโ”‚    โ”‚Tertiary โ”‚   โ”‚ Ollama  โ”‚  โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Notifications

Telegram

# Get your chat ID: message @userinfobot on Telegram
# Then configure:
context-bus config --telegram-chat-id YOUR_CHAT_ID

Example notifications:

๐Ÿ”„ [Context Bus] Opus โ†’ Codex
   Reason: Usage at 96%
   Context: Preserved โœ“
   
๐Ÿ”„ [Context Bus] Codex โ†’ Opus
   Reason: Limits reset (usage now 12%)
   Status: Idle, safe to switch โœ“

Discord

# Create a webhook in your Discord server
context-bus config --discord-webhook YOUR_WEBHOOK_URL

Slack

# Create an incoming webhook in Slack
context-bus config --slack-webhook YOUR_WEBHOOK_URL

Safety Features

Pre-Switch Checks

Before switching back to primary model:

Check What It Does
Task status Ensures no tasks are in_progress
Sub-agents Checks for active spawned sessions
Cooldown Prevents rapid toggling (5 min minimum)

If Checks Fail

  • Switch is delayed to next heartbeat
  • After 10 failed attempts, alert is sent
  • Manual override available: context-bus switch --force opus

Troubleshooting

"Usage unknown"

Run /status in your agent to check usage, then:

~/.context-bus/update-usage.sh 78

"Handoff not working"

Check files exist:

ls -la ~/.context-bus/
cat ~/.context-bus/handoff.json | jq .

"Notifications not sending"

Verify config:

context-bus config --show
# Check telegram.chat_id is set

Uninstall

curl -fsSL https://raw.githubusercontent.com/rajathkm/context-bus/main/uninstall.sh | bash

Or manually:

rm -rf ~/.context-bus
rm -rf ~/.config/context-bus

Contributing

Contributions welcome! See CONTRIBUTING.md

  1. Fork the repo
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

MIT License - see LICENSE


Credits

Built for the OpenClaw community.

Inspired by Agent Rules Sync.

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.1.0.tar.gz (24.7 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.1.0-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file context_bus-0.1.0.tar.gz.

File metadata

  • Download URL: context_bus-0.1.0.tar.gz
  • Upload date:
  • Size: 24.7 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.1.0.tar.gz
Algorithm Hash digest
SHA256 684cb7e0193edbe564eb31f8bfa319af53fc3621ff756e7c60c7070e8d5ef2b2
MD5 a3b7ce54ef288eef71f4b89295effae0
BLAKE2b-256 a009e51677ef235588d1742c5e9e42db9133456bdfd010799e786042a2ba2ac6

See more details on using hashes here.

File details

Details for the file context_bus-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: context_bus-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.8 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c89776358b9bc11b0789ccbcf3bc62e2acbb4ad0973d1dc75621fdc21a07d75
MD5 2642f89754b3c8ab0cab3f34ba1e80d1
BLAKE2b-256 edd9a391a37c52d02cd099d44f66ef2a63217ec2d6255df657fe4d2c59478a0d

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