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
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 compilefinalize()— persists final state back to the SelfReference backend after the turn ends
How They Connect
DataFromSelfRef: The Snapshot
When SelfRef is active, the compile pipeline receives aDataFromSelfRef snapshot:
- 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 boundaryforget("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:- Compaction is queued (not applied immediately)
- After the current tool batch completes, the runtime applies a summary patch
- The system prompt is preserved
- Working transcript is replaced with the summary message
- Items in
rememberbecome durable experiences - SelfReference backend stores the new state
Fork Lifecycle
Forks let an agent delegate work to child agents that inherit context:- 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 optionallyhistory
Activation
SelfRef is activated on@llm_chat via self_reference_key:
- Creates/retrieves the
SelfReferencebackend for this key - Wraps each invocation in a
SelfRefSession - Injects selfref primitives into the PyRepl runtime
- 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.