Cookbook: Custom LLM (Client-Side)
Step-by-step tutorial with full source code
New Feature: Anam now supports server-side custom LLMs where we
handle the LLM calls for you, improving latency and simplifying development. This guide shows the
client-side approach where you manage the LLM calls yourself.
What You’ll Build
By the end of this guide, you’ll have a persona application featuring:- Custom AI Brain using your own language model (OpenAI GPT-4.1-mini)
- Streaming Responses with real-time text-to-speech conversion
- Turn-taking Management that handles conversation flow
- Message History Integration that maintains conversation context
- Error Handling & Recovery for production use
This guide uses OpenAI’s GPT-4.1-mini as an example custom LLM for demonstration purposes. In
your actual application, you would replace the OpenAI integration with calls to your specific LLM
provider. The core integration pattern remains the same regardless of your LLM choice.
Prerequisites
- Node.js (version 18 or higher) and npm installed
- Understanding of modern JavaScript/TypeScript and streaming APIs
- An Anam API key (get one here)
- An OpenAI API key (get one here)
- Basic knowledge of Express.js and modern web development
- A microphone and speakers for voice interaction
Understanding the Custom LLM Flow
Before diving into the implementation, here is how custom LLM integration works with Anam personas. Regardless of your custom LLM provider, the implementation pattern follows these steps:1
Disable Default Brain
The
llmId: "CUSTOMER_CLIENT_V1" setting in the session token request disables Anam’s default AI, allowing you to handle all conversation logic.2
Listen for User Input
The
MESSAGE_HISTORY_UPDATED event fires when the user finishes speaking, providing the complete
conversation history including the new user message.3
Process with Custom LLM
Your server endpoint receives the conversation history and generates a streaming response using
your chosen LLM (OpenAI in this example).
4
Stream to Persona
The LLM response is streamed back to the client and forwarded to the persona using
createTalkMessageStream() for text-to-speech conversion.Basic Setup
Let’s start by building the foundation with custom LLM integration. This setup creates a web application with four main components:1
Create project directory
2
Initialize Node.js project
package.json file for managing dependencies.3
Create public directory
The
public folder will contain your HTML and JavaScript files that are served to the browser.4
Install dependencies
We’re installing Express for the server, dotenv for environment variables, and the OpenAI SDK
for custom LLM integration. The Anam SDK will be loaded directly from a CDN in the browser.
5
Configure environment variables
Create a
.env file in your project root to store your API keys securely:.env
Step 1: Set up your server with LLM streaming
Create an Express server that handles both session token generation and LLM streaming:server.js
The key difference here is setting
llmId: "CUSTOMER_CLIENT_V1" which disables Anam’s default AI
and enables custom LLM integration. The /api/chat-stream endpoint handles the actual AI
conversation logic.Step 2: Set up your HTML
Create a simple HTML page with video element and conversation display:public/index.html
Step 3: Implement the client-side custom LLM integration
Create the client-side JavaScript that handles the custom LLM integration:public/script.js
Step 4: Test your custom LLM integration
- Start your server:
- Open http://localhost:8000 in your browser
- Click “Start Conversation” to begin chatting with your custom LLM-powered persona!
You should see Cara appear and greet you, powered by your custom OpenAI integration. Try having a
conversation - your voice will be transcribed, sent to OpenAI’s GPT-4.1-mini, and the response will
be streamed back through the persona’s voice and video.
Advanced Features
Enhanced Error Handling
Add retry logic to improve reliability:What You’ve Built
You’ve integrated a custom language model with Anam’s persona system. Your application includes:- Custom AI Brain: Control over your persona’s intelligence using OpenAI’s GPT-4.1-mini, with the ability to customize personality, knowledge, and behavior.
- Real-time Streaming: Responses stream from your LLM through the persona’s voice.
- Conversation Context: Full conversation history is maintained and provided to your LLM for contextually aware responses.
- Error Handling: Retry logic and fallback responses for reliability.
- Extensible Architecture: The modular design allows you to swap LLM providers, add custom logic, or integrate with other AI services.
Troubleshooting
LLM Responses Not Streaming
LLM Responses Not Streaming
Symptoms: Persona doesn’t speak or responses are delayedSolutions:
- Verify OpenAI API key is correctly configured
- Check that
llmId: "CUSTOMER_CLIENT_V1"is set in session token - Ensure
MESSAGE_HISTORY_UPDATEDevent listener is properly connected - Check browser console for JavaScript errors
- Verify the
/api/chat-streamendpoint is responding correctly
Streaming Performance Issues
Streaming Performance Issues
Symptoms: Slow or choppy persona responsesSolutions:
- Optimize LLM model parameters (reduce max_tokens, adjust temperature)
- Implement response caching for common queries
- Use faster models like
gpt-4.1-miniinstead ofgpt-4 - Consider chunking large responses for better streaming
- Monitor network latency and server performance

