Skip to main content

Context Overview

This page explains how SimpleLLMFunc compiles context — the information the LLM sees at each reasoning step. The final provider-facing message list is built from invocation configuration, a base transcript, and internal runtime patches.

The Core Data Structures

ContextState

The runtime representation of a conversation’s current state:
  • messages is the base transcript for the current runtime state — assistant messages, tool results, user messages
  • data_from_selfref carries durable state from the SelfReference backend (experiences, summaries)
  • pending_mutations accumulates internal runtime patches during a ReAct iteration, applied to the transcript at the next compile boundary

CompileSource

The input boundary — everything needed to produce an LLM request:

DataFromAgentConfig

Static configuration from the decorator:

DataFromSelfRef

Durable state from the SelfReference backend:

Context Inputs

SimpleLLMFunc does not build LLM context from mutations alone. Each LLM request is compiled from three inputs:
  1. Invocation configuration — the InvocationSpec / prompt contract built from the decorated function, docstring, template parameters, tool guidance, return mode, and SelfRef snapshot.
  2. Base transcript — the message list created from history / chat_history, the current user input, and the previous finalized messages.
  3. Runtime patches — ContextMutation objects produced internally by LLM calls, tools, SelfRef primitives, compaction, and abort/cancel handling.

Data Flow: From Your Code to the LLM

The Key Invariants

  1. Runtime side effects do not edit the live transcript directly — LLM calls, tools, SelfRef primitives, and abort handling produce ContextMutation patches instead of mutating ContextState.messages in place.
  2. Mutations are transcript patches, not the whole context — docstrings, template params, tool guidance, SelfRef snapshots, and initial history come from invocation configuration and the base transcript.
  3. Mutations accumulate, then apply atomically — During a ReAct iteration, runtime patches collect in pending_mutations. They’re applied together at the compile boundary.
  4. Compile produces a snapshot — The compiled output is a clone. The original state is never mutated in place.
  5. SelfRef state propagates — DataFromSelfRef is carried forward through compilations. If selfref markers are detected in the transcript after mutation application, the snapshot is refreshed.
  6. System prompt resolution has priority order:
    • SelfRef snapshot (if present) → renders base prompt + experiences
    • PromptContract.system_prompt (if set explicitly)
    • Latest system message in transcript
    • PromptContract.base_instruction (docstring fallback)

Next Steps

Runtime Patches

The internal mutation types — when runtime effects produce transcript patches.

Compile Pipeline

How invocation config, transcript, and patches become the final LLM request.