Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI Shell Team Aish LLM Session Reconstruction

From Leeroopedia
Knowledge Sources
Domains LLM, Session_Management
Last Updated 2026-10-29 00:00 GMT

Overview

Pattern for reconstructing an LLM session from persisted credentials and configuration, restoring the API dialect and plan mode state.

Description

LLM Session Reconstruction is the mechanism that creates a new LlmSession from the session record's stored credentials (API base, API key, model). The constructor resolves the API dialect for the model and endpoint, initializes the stream context, sets up token tracking, and restores the plan mode state from the session snapshot.

The session starts with empty tools — the caller must register the appropriate tools based on the current configuration. The temperature and max_tokens are restored from the session record or defaults are applied. The cancellation token and event callback are fresh.

This pattern solves the problem of recreating the exact LLM connection context from persisted data, so the resumed session uses the same provider and model as the original.

Usage

Use this principle when resuming a session: construct the LLM session from the stored credentials, then register tools and wire up the context manager before accepting user input.

Theoretical Basis

The reconstruction is a constructor with dialect resolution:

# Abstract algorithm (NOT real implementation)
def LlmSession_new(api_base, api_key, model, temperature, max_tokens):
    stream_ctx = StreamContext(api_base, api_key, model)
    dialect = resolve_api_dialect(model, api_base, api_key)
    return LlmSession(
        stream_ctx=stream_ctx,
        api_dialect=dialect,
        tools={},
        cancellation_token=new_token(),
        token_stats=TokenStats(),
        plan_state=PlanModeState(),  # restored from snapshot
        # ...
    )

Key design ideas:

  • Dialect resolution at construction: the API dialect is determined once and is stable for the session's lifetime
  • Fresh state: cancellation, events, and tool registry are new; only credentials and config are restored
  • Plan state restoration: the plan mode state is loaded from the session snapshot

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment