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
promptfield 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:
| Processor | Role | Purpose |
|---|---|---|
context_processor | System context | Business data, account info, external state |
input_processor | User message | Cleaned, validated user input |
others_processor | Supplementary | Attachments, 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_responseDownstream agents automatically receive the structured output from upstream agents without manual data passing.
Context Strategies
| Strategy | When to Use |
|---|---|
| Pre-compose context_processor | External data that the LLM needs as background |
| Pre-compose input_processor | When raw input needs cleaning or extraction |
| Intelligence function + session state | When you need to read/write state during execution |
| Tool calling | When the LLM needs to decide what data to fetch |
| Upstream yields | When agents form a processing chain |
Context Boundaries
Each kind of context has its own lifetime and its own way in:
| Context Type | Lifetime | Access |
|---|---|---|
| Session state | REDIS_SESSION_TTL (7 days default) | Studio Sessions tab |
| Agent yields | REDIS_DEFAULT_TTL (1h default) | Auto-passed via input_data |
| Mesh communications | REDIS_DEFAULT_TTL | Managed automatically |
| Conversation history | REDIS_SESSION_TTL | Studio Sessions tab |
| Long-term data | Custom | Your application code (external database) |
Next Steps
- Message Formatting — How messages are constructed
- Middleware Overview — Pre-compose pipeline
- State Management — Session state patterns
LeafMesh — Structured context assembly