Context Window

In Turn, the context window is not an array you append strings to. It is a resource — finite, managed by the VM, with automatic eviction policies. This is one of Turn's most important innovations.


The Context Problem

Every LLM agent framework eventually hits the same wall: the model's context limit. When you hit it, you either crash with a context_length_exceeded error, or you silently truncate from the front of history and hope the model doesn't notice the discontinuity.

Both outcomes are failures. Turn solves this at the language level.


context.append — Writing to the Context Window

syntax
context.append(expression);

Appends a string to the agent's context window. Every call is token-metered — the VM tracks how many tokens the context window has consumed against the configured budget.

example
context.append("You are a senior financial analyst. Be precise and cite data.");
context.append("Client portfolio: " + portfolio_summary);
context.append("User question: " + user_query);

// The LLM will receive all three in order when infer is called
let result = infer Analysis { "Provide your recommendation."; };

The Priority Stack

Turn's context window is not a flat queue — it is a Priority Stack with four layers:

PriorityNameDescriptionEviction Policy
P0System / MissionCore agent instructions. E.g. "You are a financial analyst."Never evicted
P1Plan / ScratchpadCurrent working plan and intermediate reasoningSummarized when budget approached
P2HistoryPast conversation turns and tool outputsDropped oldest-first
P3UserThe current user input / promptProtected for current turn

When context.append() would exceed the token budget, the VM applies the Entropic Expansion Policy:

  1. Drop oldest P2 (history) entries first
  2. Summarize P1 (scratchpad) via a secondary infer call if still over budget
  3. P0 (system) is never evicted — the agent's core instructions are always present
InvariantContext Budget Invariant

context.append() never silently overflows. The VM enforces the token budget via the Priority Stack and Entropic Expansion Policy. Context overflow is a structured, handled event — not a crash or a silent truncation.


Context vs. Memory

This distinction is critical:

mental model
context  =  the agent's "working memory" for THIS TURN
           token-bounded, cleared between turns
           what the model can see right now

memory   =  the agent's LONG-TERM KNOWLEDGE
           unlimited, persisted across restarts
           retrieved semantically via HNSW before each infer call

Think of context as a whiteboard — you write on it, the model reads it, and you erase it at the end of the session. Memory is a filing cabinet — permanent, semantic, and automatically searched for relevance before you present the whiteboard.


Context Is Per-Process

Every spawned agent has its own isolated context window. When you spawn a child process, it starts with an empty context:

context_isolation.tn
context.append("Parent context: I am the orchestrator.");

let child = spawn {
  // This process starts fresh — the parent's context is NOT inherited
  context.append("Child context: I am the analyst.");
  let result = infer Summary { "Summarize my role."; };
  return result;
};

// The parent's infer calls see "orchestrator" context
// The child's infer calls see "analyst" context
// Fully isolated — no accidental context bleeding between agents

Configuring the Token Budget

The token budget defaults to 1,000,000 units. Each instruction in the VM consumes 1 unit; HNSW semantic searches consume 50 units; explicit tool calls consume variable amounts.

The budget acts as a gas meter — a mechanism to prevent infinite loops and runaway agents. When the budget reaches zero, the VM halts the process cleanly.

TIP

The token budget is not the same as the LLM's context window size. The VM budget is a computational gas counter for the Turn VM itself. The LLM token limit is enforced by the inference provider. Both are independent constraints.


Next Steps

  • Memory — The permanent, semantic counterpart to context
  • Runtime Model — Full agent state including context, memory, env, and mailbox
  • The infer Primitive — How context feeds into inference calls