Skip to main content
Cedar-OS provides comprehensive end-to-end type safety for agent requests. There are three main areas where you can implement type safety:
  1. The type of additionalContext - Define TypeScript types for context data (user profiles, projects, etc.)
  2. The type of additional params - Define custom fields for your specific backend needs
  3. Frontend tool and state setter schemas - Define Zod schemas for tool arguments and state setter parameters
For all customization points, Cedar exposes:
  • TypeScript types for compile-time checking
  • Zod schemas for runtime validation and schema factories

Additional Params Typing using E

The E generic parameter allows you to define custom fields that get passed directly to your backend.

Configurable Providers (Support Custom Fields)

Configurable providers like Mastra and Custom backends allow you to define additional parameters: TypeScript Types:
Corresponding Zod Schemas:
Usage Example:

Standardized Providers (Fixed APIs)

Standardized providers have fixed APIs and don’t support custom fields: TypeScript Types:
Corresponding Zod Schemas:

Additional Context Typing using T

The T generic parameter allows you to define TypeScript types for your React state data that gets sent back in the additionalContext field.

What your backend receives in additionalContext

Your backend receives the transformed context with Cedar system fields added and UI metadata stripped. Each context item becomes a simple object with data and source fields: TypeScript Types:
Corresponding Zod Schema:

Typing sendMessage

The sendMessage function (used by Cedar’s chat interface) and callLLM function both accept the same typed parameters. You can pass your MyMastraRequest type object to either:
Both functions use the same two-generic typing system and pass your typed request to the backend for validation.

Frontend Tool and State Setter Schema Typing

Cedar has comprehensive Zod schema support for frontend tools and state setters, providing runtime validation and automatic schema generation for agents. You can pick any schema off of the additionalContext object and pass it as a JSON schema to an agent’s output schema.

Frontend Tool Schema Typing

State Setter Schema Typing

Schema Benefits

Using Zod schemas for frontend tools and state setters provides:
  1. Runtime Validation: Arguments are validated before execution
  2. Type Safety: Full TypeScript support with inferred types
  3. Agent Context: Schemas are automatically converted to JSON Schema and sent to agents
  4. Error Handling: Clear validation error messages
  5. Documentation: Schema descriptions help agents understand parameters

End-to-End Example

Here’s a simplified example showing the complete type safety pattern:

Next Steps

Now that you understand request typing, learn about: