Skip to main content

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)
The system automatically handles:
  • Request/response logging
  • Error handling and retries
  • Stream management and cancellation
  • Type safety and validation

Provider Interface

Every custom provider must implement the ProviderImplementation 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:
Expected Output:
Example Implementation:

2. callLLMStructured - Structured Output Calls

Purpose: Make calls that return structured data (JSON) based on a provided schema. Input Parameters:
Expected Output: Same as 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 as callLLM
  • handler: A callback function to process stream events
Stream Handler Types:
Expected Output:
Example Implementation:

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 in packages/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 using CustomParams<T, E>:
The CustomParams type allows you to:
  • T: Define types for your additionalContext data
  • E: Define types for custom fields specific to your backend
For complete type safety implementation, validation with Zod schemas, and detailed examples, see Typing Agent Requests.

Best Practices

  1. Error Handling: Always handle network errors, API errors, and parsing errors gracefully
  2. Abort Signals: Support cancellation in streaming operations using AbortController
  3. Type Safety: Use TypeScript interfaces for better development experience
  4. Logging: The system automatically logs requests/responses, but you can add custom logging
  5. Configuration: Make your provider configurable through the config object
  6. Testing: Test all functions thoroughly, especially streaming and error scenarios

Troubleshooting

Provider not found: Make sure you’ve registered your provider in the provider registry. Type errors: Ensure your parameter and config types extend the required base interfaces. Streaming issues: Check that your API supports Server-Sent Events and that you’re parsing the format correctly. Authentication errors: Verify your API key and authentication method match your provider’s requirements.