How It Works
When a user sends a message, Cedar automatically gathers context from various sources:- Subscribed States: Application state you’ve made available to the agent via
useCedarState
and subscribed to viauseSubscribeStateToAgentContext
- Mentions: Specific data the user has referenced with @ mentions (registered using
useStateBasedMentionProvider
) - Additional Context: Context entries you’ve manually added (via
addContextEntry
) - Chat Input Content: The current editor content with mentions
Adding Context
Prerequisite – Before you can add any context or mentions you must register the relevant React state in Cedar using eitheruseRegisterState
oruseCedarState
. All examples below assume that registration step has already happened (or they demonstrate it explicitly).
Mentions System
Cedar’s mention system allows users to reference specific data using @ symbols. You can create mention providers that let users easily reference state data:State Subscription
Keep a Cedar state continuously in sync with the agent input context viauseSubscribeStateToAgentContext
:
Advanced
This is for advanced understanding. Move on to Agentic State Access or Spells for better functionality.Default Context Tokenization
Cedar’ssendMessage
function automatically handles context based on your provider configuration:
For Structured Providers (Mastra, Custom)
For Text-Based Providers (OpenAI, Anthropic, AI-SDK)
Backend Integration Examples
Mastra Backend Example
Custom Provider Backend Example
Accessing Additional Context
You can access the context data in several ways:Raw Context Access
Stringified Context Access
Adding Specific Context
Manual Context Entries
Add context entries programmatically for specific use cases:Conditional Context
Add context based on application state or user actions:Custom Context Processing
Create your own message sending logic with custom context filtering:Next Steps
- Learn about mentions functionality for detailed @ mention patterns
- Explore subscribing to state for automatic state synchronization