Context Engineering

Context Engineering

Context engineering in LeafMesh is the practice of assembling the right information in the right format before sending it to the LLM. The framework provides structured mechanisms for this through the pre-compose pipeline and automatic yields propagation.

How Context Is Assembled

For each LLM agent, LeafMesh assembles the prompt from several sources:

  • Agent prompt — the prompt field from your YAML
  • Yields schema — automatically appended so the LLM knows exactly what structured output to produce
  • Pre-compose output — context, input, and others processors (see below)
  • Conversation history — recent turns from the session, automatically capped to keep prompts within budget
  • Upstream yields — the calling agent's structured output, passed as input_data

You do not assemble messages by hand. The framework handles role assignment (system / user / supplementary), history truncation, and yields schema injection.

History Management

Conversation history is automatically stored per session and the most recent turns are included in the prompt. Older turns remain in session storage for retrieval but are excluded from new prompts so context-window budgets stay under control.

History records carry role (user / assistant / system), content, timestamp, and the agent that produced each message — visible in the Studio Sessions tab.

Pre-Compose Context Assembly

Pre-compose processors run before the LLM sees any data. Each maps to a distinct role in the prompt:

ProcessorRolePurpose
context_processorSystem contextBusiness data, account info, external state
input_processorUser messageCleaned, validated user input
others_processorSupplementaryAttachments, metadata, additional data

This separation keeps context organized and prevents information from being mixed into the wrong role.

Yields Schema Injection

The framework automatically injects the agent's yields schema into the system context, telling the LLM exactly what format to produce:

agents: classifier: name: "classifier" model: "gpt-4o-mini" # required — the SDK never picks one for you prompt: "Classify the incoming request." yields: category: "string" urgency: "number" summary: "string"

The LLM sees the yields schema as part of its instructions, guiding it to produce structured output that matches the declared format.

Context from Upstream Agents

When an agent is called via can_call, the upstream agent's yields are passed as input_data:

async def specialist(llm_response, input_data, context): # input_data contains the upstream agent's yields upstream_category = input_data.get("category") upstream_urgency = input_data.get("urgency") return llm_response

Downstream agents automatically receive the structured output from upstream agents without manual data passing.

Context Strategies

StrategyWhen to Use
Pre-compose context_processorExternal data that the LLM needs as background
Pre-compose input_processorWhen raw input needs cleaning or extraction
Intelligence function + session stateWhen you need to read/write state during execution
Tool callingWhen the LLM needs to decide what data to fetch
Upstream yieldsWhen agents form a processing chain

Context Boundaries

Each kind of context has its own lifetime and its own way in:

Context TypeLifetimeAccess
Session stateREDIS_SESSION_TTL (7 days default)Studio Sessions tab
Agent yieldsREDIS_DEFAULT_TTL (1h default)Auto-passed via input_data
Mesh communicationsREDIS_DEFAULT_TTLManaged automatically
Conversation historyREDIS_SESSION_TTLStudio Sessions tab
Long-term dataCustomYour application code (external database)

Next Steps


LeafMesh — Structured context assembly

Last updated: 11/10/2026