Use this file to discover all available pages before exploring further.
Cedar provides a flexible system for creating custom agent flows that can work with different backend providers. This guide explains how Cedar prepares requests, routes calls, and handles responses from your agent backend.
Before making any call to your agent backend, Cedar automatically assembles all the necessary context and data. This preparation process ensures your agent receives comprehensive information to generate meaningful responses.
1
User prompt extraction
Cedar extracts the current user input from the chat editor, converting the rich text content into a clean string format.
// Cedar automatically calls stringifyEditor() to extract textconst editorContent = state.stringifyEditor();// Result: "What's the status of the user authentication feature?"
2
State and context gathering
Cedar collects all subscribed state and additional context that has been registered with the agent input system.
// Cedar automatically gathers additional contextconst additionalContext = state.compileAdditionalContext();// Result includes: subscribed state, custom setters, mention data, etc.
The agentContextSlice manages all context gathering. See Agent Input Context for details on how to subscribe state and provide context to your agents.
3
Context tokenization
Cedar serializes the collected context into a structured format that your agent backend can parse and understand.
// Example of tokenized additional context{ "todos": [ { "id": "1", "title": "Fix login bug", "completed": false } ], "setters": { "addTodo": { "name": "addTodo", "stateKey": "todos", "description": "Add a new todo item", "schema": { "type": "object", "properties": { "title": { "type": "string", "description": "The title of the todo item" } }, "required": ["title"] } } }}
4
Network Request Body Structure
The network request body varies significantly between providers, allowing Cedar to work with different backend architectures while maintaining a consistent internal API.For mastra and Custom backends, we pass in additionalContext, state, and more as seperate fields to give you control over what you want to do with the context and state.For direct LLM calls, we automatically tokenise the additionalContext & prompt into one string for optimal responses.
{ "prompt": "What's the status of the user authentication feature?", "systemPrompt": "You are a helpful project manager...", "additionalContext": { "todos": [...], "setters": {...} }, "states": { "emails:": [...] }, "route": "/chat", "resourceId": "user123", "threadId": "thread456",}
Beyond using Cedar’s built-in agent connections, you can create custom AI workflows that leverage Cedar’s context system while implementing your own processing logic.
1
Define your prompt
Create a function that defines a specific prompt for your workflow. This allows you to customize the AI’s behavior for your specific use case.
function createSummaryWorkflow() { const systemPrompt = `You are a project summary assistant. Analyze the provided todos and create a concise project status report. Focus on completion rates and upcoming priorities.`; return systemPrompt;}
2
Extract specific additional context
Use Cedar’s context system to get exactly the data your workflow needs. You can filter and transform the context to match your requirements.
function getProjectContext(state: CedarState) { const additionalContext = state.compileAdditionalContext(); // Extract only the data relevant to project summaries const projectContext = { todos: additionalContext.todos || [], deadlines: additionalContext.deadlines || [], teamMembers: additionalContext.teamMembers || [], }; return projectContext;}
3
Call the LLM
Make the API call to your chosen LLM provider with your custom prompt and filtered context.
Cedar-OS now supports fully typed backend integrations with generic sendMessage parameters. You can specify custom field types and additional context types for enhanced type safety:
This provides both compile-time type checking and runtime validation when used with the corresponding Zod schemas.
For more advanced response processing patterns and custom response handlers,
see Custom Response
Processing to learn how
to create sophisticated response processors that can handle streaming,
actions, and complex data transformations.