Skip to main content
Learn how to bypass Anam’s built-in language models and integrate your own custom LLM for complete control over conversation logic. This guide uses OpenAI as an example, but the pattern works with any LLM provider (Anthropic, Google Gemini, Groq, Mistral, etc.).

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
After completing the initial setup (Steps 1-4), you can extend this foundation by adding features like conversation memory, different LLM providers, custom system prompts, or specialized AI behaviors.
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.
Using these core concepts, we’ll build a simple web application that allows you to chat with your custom LLM-powered persona.

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

This creates a 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
Replace the placeholder values with your actual API keys. Never commit this file to version control.

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

  1. Start your server:
  1. Open http://localhost:8000 in your browser
  2. 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

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_UPDATED event listener is properly connected
  • Check browser console for JavaScript errors
  • Verify the /api/chat-stream endpoint is responding correctly
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-mini instead of gpt-4
  • Consider chunking large responses for better streaming
  • Monitor network latency and server performance

Last modified on June 26, 2026