Custom Backend Implementation
Cedar-OS provides a flexible agent connection system that allows you to integrate with any LLM provider or custom backend. This guide explains how to implement a custom provider by creating the required functions and registering them with the system.Agent Connection Architecture
The Cedar-OS agent connection system is built around a provider pattern that abstracts different LLM services behind a common interface. Each provider implements a set of standardized functions that handle:- Non-streaming LLM calls (
callLLM
) - Structured output calls (
callLLMStructured
) - Streaming responses (
streamLLM
) - Response parsing (
handleResponse
)
- Request/response logging
- Error handling and retries
- Stream management and cancellation
- Type safety and validation
Provider Interface
Every custom provider must implement theProviderImplementation
interface with these 5 required functions:
Required Function Implementations
1. callLLM
- Basic LLM Calls
Purpose: Make non-streaming calls to your LLM service.
Input Parameters:
2. callLLMStructured
- Structured Output Calls
Purpose: Make calls that return structured data (JSON) based on a provided schema.
Input Parameters:
callLLM
, but with the object
field populated with parsed structured data.
Example Implementation:
3. streamLLM
- Streaming Responses
Purpose: Handle real-time streaming responses from your LLM service.
Input Parameters:
- Same
params
ascallLLM
handler
: A callback function to process stream events
4. handleResponse
- Parse API Responses
Purpose: Convert your API’s response format to the standard LLMResponse
format.
Input: Standard Response
object from fetch
Output: LLMResponse
object
Example Implementation:
Complete Custom Provider Example
Here’s a complete example of a custom provider implementation:Registering Your Custom Provider
After implementing your provider, you need to register it with the Cedar-OS system:1. Add to Provider Registry
Update the provider registry inpackages/cedar-os/src/store/agentConnection/providers/index.ts
:
2. Configure the Provider
Set up your custom provider configuration:3. Use the Provider
Once configured, you can use your custom provider like any other:Helper Utilities
Cedar-OS provides several utility functions to help with common tasks:Event Stream Handling
For processing Server-Sent Events streams:Type Safety
Custom backends support full end-to-end type safety usingCustomParams<T, E>
:
CustomParams
type allows you to:
- T: Define types for your
additionalContext
data - E: Define types for custom fields specific to your backend
Best Practices
- Error Handling: Always handle network errors, API errors, and parsing errors gracefully
- Abort Signals: Support cancellation in streaming operations using
AbortController
- Type Safety: Use TypeScript interfaces for better development experience
- Logging: The system automatically logs requests/responses, but you can add custom logging
- Configuration: Make your provider configurable through the config object
- Testing: Test all functions thoroughly, especially streaming and error scenarios