Contexts
Managing state, memory, and behavior for agent interactions.
In Daydreams, a Context represents a specific scope of interaction or task for an agent. It encapsulates the state, memory, available actions, and behavior relevant to that scope. Think of it like a dedicated workspace for the agent to handle a particular conversation, process, or goal.
For example, you might have separate contexts for:
- Handling a Discord channel (
discord:channel
). - Managing a Telegram chat (
telegram:chat
). - Executing a specific trading strategy (
tradingContext
). - Processing a user's request in a CLI session (
cli
).
Each running instance of a context maintains its own state and memory, isolated from others.
Defining a Context
Contexts are defined using the context
function exported from
@daydreamsai/core
.
Key Parameters:
type
(string): A unique identifier for the kind of context (e.g., "discord:channel", "trading").schema
(Zod Schema): Defines the arguments needed to uniquely identify an instance of this context (e.g.,{ channelId: z.string() }
). The agent uses these arguments to load or create the correct context state.key
(Function, optional): Generates a unique string identifier for a context instance based on its arguments. If omitted, thetype
is used (suitable if only one instance of that type exists).setup
(Function, optional): Runs once when a context instance is first initialized. Useful for fetching initial data needed by the context, which becomes available inContextState.options
.create
(Function, optional): Defines the initial structure of the context's persistent memory (TMemory
) if no saved state is found. The return value is stored and managed by theMemoryStore
.instructions
/description
(string | Function, optional): Provides guidance to the LLM about the context's purpose or state. Can be dynamic based on the currentContextState
.render
(Function, optional): Determines how the context's current state (ContextState.memory
) is represented in the LLM prompt (within the<context>
tag).onStep
/onRun
/shouldContinue
/onError
(Functions, optional): Lifecycle hooks executed during anagent.run
.inputs
/outputs
/actions
(Objects/Array, optional): Define I/O and actions specifically available within this context type. These are merged with the agent's global definitions.model
(LanguageModelV1, optional): Override the agent's default model for runs within this context.maxSteps
/maxWorkingMemorySize
(number, optional): Set default limits for runs within this context.
Context State (ContextState
)
When a context is active during an agent.run
, its state is represented by the
ContextState
object. This object is passed to various handlers and functions
(like render
, onStep
, action handlers).
Working Memory
Each context run (agent.run
) has an associated WorkingMemory
. This is
distinct from the persistent ContextState.memory
. Working Memory holds the
temporary logs (InputRef
, OutputRef
, ActionCall
, ActionResult
,
Thought
, etc.) generated during a single run. It's the agent's short-term
scratchpad for the current execution cycle and is used to build the prompt for
the LLM at each step.
See the Agent Lifecycle for how Working Memory is used in prompt generation.
Agent Interaction
You typically interact with contexts via the Agent
instance:
agent.getContext({ context: myContext, args: { ... } })
: Retrieves an existingContextState
instance or creates a new one if it doesn't exist. Loads memory from theMemoryStore
.agent.getContextById(id)
: Retrieves aContextState
by its full ID (e.g., "discord:channel:12345").agent.saveContext(contextState, optionalWorkingMemory)
: Persists theContextState
(including itsmemory
) to the configuredMemoryStore
.WorkingMemory
is usually saved internally duringagent.run
.agent.run({ context: myContext, args: { ... } })
: Starts or continues the execution loop for a specific context instance.
Contexts provide the essential structure for managing scoped state, behavior, and memory within the Daydreams framework.