withContext
Attach context layers to descendant steps without creating an isolated context.
Quick Example
import { callModel, withContext } from '@noetic-tools/core';
const withLayers = withContext({
id: 'with-knowledge',
child: callModel({
id: 'researcher',
model: 'openai/gpt-4o',
instructions: 'Research the topic thoroughly.',
}),
context: [knowledgeLayer, steeringLayer],
});What It Does
withContext attaches context layers to a subtree of steps without creating an isolated context. Like React's Context.Provider, layers become available to all descendant steps -- the child shares the parent's ItemLog and conversation history.
This is the primitive for scoping context layers to a section of your execution tree. Any StepCallModel descendant will have access to the provided layers during recall, store, and steering hooks.
spawn and detachedSpawn break the inheritance chain. If a descendant step spawns a child, the provided layers do not propagate into the spawn boundary.
API Reference
| Property | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Unique step identifier |
child | Step<TContext, I, O> | Yes | The step to execute with the provided layers |
context | ContextConfig | ContextLayer[] | Yes | Context layers available to all descendant steps |
Example: Scoped Knowledge Layer
Attach a knowledge base layer only to a specific subtree, without affecting sibling steps:
import { callModel, withContext } from '@noetic-tools/core';
const researchPhase = withContext({
id: 'research-with-knowledge',
child: {
kind: 'loop',
id: 'research-loop',
steps: [callModel({
id: 'researcher-llm',
model: 'openai/gpt-4o',
instructions: 'You are a research assistant.',
tools: [searchTool],
})],
until: until.noToolCalls(),
maxIterations: 10,
},
context: [semanticRecallLayer, steeringLayer],
});Related
- spawn -- context isolation with spawn-local context layers.
- Context Layers -- context layer system overview.
- Overview -- how withContext fits into the step primitives.