Skip to content

efekurucay/model-subsession-protocol

Repository files navigation

💬 Model Subsession Protocol

A open standard for nested sub-sessions in LLM chat interfaces.
Keep your context clean. Isolate topics. Never lose the big picture.

Status License Version PRs Welcome


The Problem

Every LLM chat interface today uses a single, flat context window. You start a conversation, ask many questions on different topics, and the entire history keeps growing — token by token.

This leads to real problems:

  • Context bloat: Unrelated past messages pollute every new query
  • Loss of focus: The model's attention dilutes across irrelevant history
  • Manual workarounds: Users copy-paste prompts, start new chats, manually summarize — just to manage context
  • No standard: Every platform handles this differently (or not at all)

You're studying calculus. After 20 messages you switch to physics. But the entire calculus thread is still being sent to the model — for every physics question.


The Solution: Sub-Sessions

The Model Subsession Protocol (MSP) proposes a lightweight, open standard for nested topic sessions within a single parent chat.

A sub-session is:

  • Isolated: Its messages don't pollute the parent context by default
  • Contextually aware: It can selectively pull summaries or recent messages from the parent
  • Summarizable: When closed, it produces a compact summary that can optionally be merged back
  • Composable: Sub-sessions can reference each other via summaries
Main Session
├── [messages 1-20] ← Topic: Calculus
├── SubSession: "calculus-limits" (isolated)
│   ├── msg: "what is a limit?"
│   └── msg: "explain epsilon-delta"
├── [messages 21-35] ← Topic: Physics
└── SubSession: "physics-kinematics" (isolated)
    ├── msg: "what is velocity?"
    └── msg: "solve this kinematics problem"

Installation & Usage

TypeScript/Node.js

git clone https://github.com/efekurucay/model-subsession-protocol.git
cd model-subsession-protocol/packages/core
npm install
npm run build

Quick Example

import { SessionManager, ContextBuilder } from '@model-subsession-protocol/core';

// 1. Create a session
const session = new SessionManager('study-session');

// 2. Add main messages
session.addMessage('user', 'I need help studying for my exam.');
session.addMessage('assistant', 'What subject?');

// 3. Open a subsession for focused work
const calculus = session.openSubsession('calculus-review');
calculus.addMessage('user', 'Explain limits');
calculus.addMessage('assistant', 'A limit is...');

// 4. Build context for LLM call
const context = new ContextBuilder(session, calculus).buildForLLM();

// 5. Close and summarize
calculus.close('Covered limit fundamentals');
calculus.mergeToParent();

See examples/ for complete working demos.


How It Works

1. Context Merge Policy

When a sub-session is active and the user sends a query, the model receives a merged context built from three layers:

Layer Content Default Behavior
Sub-context Persistent rules, goals, constraints, decisions Always included
Summary Snapshot Auto-generated summaries of past main-session blocks Included (compact)
Recent Messages Last N messages from the parent session Included (configurable)
Sub-session messages Current sub-session's own thread Always included

Older main-session messages beyond the snapshot window are not sent to the model.

2. Message Visibility

Each message carries a visibility field that controls context inclusion:

  • main_only — visible only in the parent session
  • subsession_only — stays inside the sub-session, never leaks to parent
  • global — visible everywhere (used for decisions, constraints)

3. Lifecycle

open_subsession() → exchange messages → close_subsession()
    ↓
  Optional: generate summary
    ↓
  Optional: merge into parent

Data Schema

Full JSON Schema definitions are in schema/.

Session Object

{
  "session_id": "main-abc123",
  "created_at": "2026-03-21T18:00:00Z",
  "sub_context": {
    "intent": "Studying for university exams",
    "constraints": ["Explain simply", "Use examples"],
    "decisions": [],
    "facts": []
  },
  "summary_snapshots": [
    {
      "id": "snap-001",
      "covers_messages": [0, 20],
      "created_at": "2026-03-21T18:10:00Z",
      "content": "Covered limits in calculus: definition, notation, and epsilon-delta proof."
    }
  ],
  "subsessions": ["sub-xyz789"]
}

Subsession Object

{
  "subsession_id": "sub-xyz789",
  "parent_session_id": "main-abc123",
  "label": "calculus-limits",
  "status": "closed",
  "created_at": "2026-03-21T18:05:00Z",
  "closed_at": "2026-03-21T18:09:00Z",
  "context_policy": {
    "include_sub_context": true,
    "include_snapshots": true,
    "recent_parent_messages": 5
  },
  "summary": "Explored epsilon-delta definition of limits.",
  "messages": [
    {
      "id": "msg-001",
      "role": "user",
      "content": "What is a limit in calculus?",
      "visibility": "subsession_only",
      "timestamp": "2026-03-21T18:05:10Z"
    }
  ]
}

See spec/SPEC.md for the complete protocol specification.


UI/UX Design Proposal

A compliant interface should provide:

  • Subsession panel — sidebar or inline list of all sub-sessions within the current chat
  • Mini chat view — when a sub-session is selected, a nested chat area opens within the main interface
  • Context indicator — shows how much parent context is being pulled in (token count)
  • Close & summarize button — closes the sub-session and optionally appends its summary to the parent
  • Visibility toggle — user can mark individual messages as global to promote them to the parent context
┌─────────────────────────────────────────────┐
│ Main Chat                                    │
│ ───────────────────────────────────────────  │
│ [msg] [msg] [msg] [msg]                      │
│                                               │
│   ┌──────────────────────────────────────┐  │
│   │ 📎 SubSession: calculus-limits       │  │
│   │ ──────────────────────────────────   │  │
│   │ [msg] [msg] [msg]                    │  │
│   │ > _                                  │  │
│   │ [Close & Summarize]  [Discard]       │  │
│   └──────────────────────────────────────┘  │
│                                               │
│ > _                                          │
└─────────────────────────────────────────────┘

Use Cases

Scenario How MSP Helps
Studying multiple subjects Each topic gets its own sub-session. Summaries accumulate without bloating context.
Software development Debugging a specific file in a sub-session while the main thread tracks overall architecture.
Research & writing Deep-dive research in a sub-session, then merge findings back to the main draft.
Multi-step planning Explore alternatives in sub-sessions before committing decisions to the main context.
Long interviews / forms Each section handled in isolation.

Compatibility

MSP is model-agnostic and platform-agnostic. It operates purely at the context shaping layer — before the LLM call is made.

  • Works with OpenAI, Anthropic, Google Gemini, Mistral, local models (Ollama, LM Studio)
  • Can be built on top of Anthropic MCP as a context management layer
  • No changes required to the underlying LLM API

Roadmap

Core Protocol & Reference Implementation

  • v0.1.0 Protocol Specification — Formal spec with data model, lifecycle, and context policies
  • JSON Schema Validation — Draft-07 schemas for session, subsession, and message objects
  • TypeScript Reference Library@model-subsession-protocol/core with SessionManager, ContextBuilder, and utilities
  • Unit Tests — Full test coverage using Node.js built-in test runner
  • CI/CD Pipeline — GitHub Actions for automated build and test
  • Contributing Guide — Clear setup and contribution workflow
  • npm Package Publishing — Publish @model-subsession-protocol/core@0.1.0 to npm registry
  • Documentation Site — Dedicated docs with API reference and interactive examples

Advanced Features

  • Snapshot Auto-Generation — LLM-powered summarization of message ranges
  • Token Budget Management — Smart context window optimization based on model limits
  • Nested Subsessions — Allow subsessions within subsessions for deep hierarchies
  • Cross-Session References — Link subsessions across different parent sessions
  • Conflict Resolution — Merge strategies when subsession summaries conflict with parent context
  • Visibility Inference — Auto-detect message importance and suggest visibility levels

Integrations & SDKs

  • Python SDKmodel-subsession-protocol package for Python
  • MCP Server — Model Context Protocol server implementation for Claude Desktop and compatible clients
  • OpenAI Functions/Tools — Native integration via function calling
  • LangChain Adapter — Custom chain/memory component
  • LlamaIndex Module — Index and query subsessions as separate documents

Platform Integrations

  • Open WebUI Plugin — Native subsession support in Open WebUI
  • LibreChat Integration — Custom preset or plugin
  • ChatGPT Custom GPT — Protocol implementation via actions
  • VS Code Extension — Subsession management for Copilot Chat
  • Raycast Extension — Command palette for subsession workflows

UI Components & Examples

  • React Component Library@model-subsession-protocol/react with pre-built UI widgets
  • Web Demo — Interactive playground to test the protocol
  • CLI Tool — Terminal-based subsession manager
  • Mobile App (React Native) — Reference implementation for iOS/Android

Community & Ecosystem

  • RFC Process — Formalized improvement proposal system
  • Community Showcase — Gallery of projects using MSP
  • Plugin Registry — Centralized directory of MSP integrations
  • v1.0 Stable Release — Production-ready spec with backward compatibility guarantees

Contributing

This is an open proposal. Contributions, critiques, and alternative designs are very welcome.

  1. Fork the repo
  2. Create a branch: git checkout -b feature/my-proposal
  3. Commit your changes
  4. Open a Pull Request

For major proposals, please open an Issue first to discuss the idea.

See CONTRIBUTING.md for detailed setup and development guidelines.


License

MIT — free to use, modify, and build upon.


If this solves a problem you've felt, give it a ⭐ and help spread the word.

Made with frustration and coffee by @efekurucay

About

A protocol/standard for nested sub-sessions in LLM chat interfaces. Keeps context clean by isolating topic-specific conversations within a main session.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors