Skip to main content

SelfRef

SelfRef is SimpleLLMFunc’s system for durable, self-modifying context — letting agents remember across turns, compress their history, and delegate work to child agents.

Two Components, Sharp Separation

SelfReference (durable backend)

The stateful storage layer. Lives across invocations. Holds:
  • History per memory key — the full conversation transcript
  • Experiences — durable remembered facts/lessons
  • Summaries — compaction checkpoints
  • Fork state — child agent handles and results
Since 0.8.1, SelfReference is implemented as a small public facade over focused internal components: This split does not change the public SelfReference API; it makes the durable backend easier to maintain and reason about.

SelfRefSession (invocation-scoped plugin)

The per-call lifecycle adapter. Created fresh for each @llm_chat invocation. Implements ReAct hooks:
  • collect_context_mutations() — provides selfref-originated internal transcript patches before each compile
  • finalize() — persists final state back to the SelfReference backend after the turn ends
The session bridges the gap between the stateless ReAct loop and the stateful backend.

How They Connect

DataFromSelfRef: The Snapshot

When SelfRef is active, the compile pipeline receives a DataFromSelfRef snapshot:
This snapshot determines:
  • The system prompt (base + rendered experiences)
  • What messages the LLM sees (working_messages after compaction)
  • What experiences are active

The 6 Runtime Primitives

When a @llm_chat agent has SelfRef enabled and uses PyRepl, these primitives are available inside execute_code:

Context Primitives

Fork Primitives

Experience Lifecycle

Experiences are durable facts stored in the system prompt:
  • remember("...") → records an experience through the runtime patch boundary
  • forget("exp_001") → removes it
  • Experiences survive compaction — they’re stored in the system prompt, not the working transcript
  • They’re rendered by render_system_prompt_with_experiences() during compile Stage 2

Compaction Lifecycle

When context grows too large, the agent can compact:
What happens:
  1. Compaction is queued (not applied immediately)
  2. After the current tool batch completes, the runtime applies a summary patch
  3. The system prompt is preserved
  4. Working transcript is replaced with the summary message
  5. Items in remember become durable experiences
  6. SelfReference backend stores the new state

Fork Lifecycle

Forks let an agent delegate work to child agents that inherit context:
Key rules:
  • Children inherit the pre-fork context snapshot (not the parent’s in-flight state)
  • Children cannot modify the parent’s context
  • gather_all() blocks until all children complete
  • Results contain status, response, and optionally history

Activation

SelfRef is activated on @llm_chat via self_reference_key:
The framework automatically:
  1. Creates/retrieves the SelfReference backend for this key
  2. Wraps each invocation in a SelfRefSession
  3. Injects selfref primitives into the PyRepl runtime
  4. Handles finalization (persist updated history) after each turn

Building Guide: llm_chat

How to use @llm_chat with SelfRef in practice.

Advanced: SelfRef Engineering

Advanced patterns: multi-key memory, compaction strategies, fork orchestration.