Skip to main content
Agent Context conrols the additional information to your AI agents beyond just the user’s message. This includes application state, user data, conversation history, and any other contextual information that helps the agent provide better responses. The underlying source of truth for this information is Cedar state, so it is helpful to understand how that works first.

How It Works

When a user sends a message, Cedar automatically gathers context from various sources:
  1. Subscribed States: Application state you’ve made available to the agent via useCedarState and subscribed to via useSubscribeStateToAgentContext
  2. Mentions: Specific data the user has referenced with @ mentions (registered using useStateBasedMentionProvider)
  3. Additional Context: Context entries you’ve manually added (via addContextEntry)
  4. Chat Input Content: The current editor content with mentions
Cedar provides several methods to compile and access this context for your AI agents.

Adding Context

Prerequisite – Before you can add any context or mentions you must register the relevant React state in Cedar using either useRegisterState or useCedarState. 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:
For detailed information about mentions, including custom providers, multiple triggers, and advanced rendering, see the Mentions documentation.

State Subscription

Keep a Cedar state continuously in sync with the agent input context via useSubscribeStateToAgentContext:
For comprehensive examples and best practices for state subscription, see the Subscribing State documentation.

Advanced

This is for advanced understanding. Move on to Agentic State Access or Spells for better functionality.

Default Context Tokenization

Cedar’s sendMessage 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