Retell AI SDK Patterns
Overview
Production-ready patterns for Retell AI: client singletons, typed agent configurations, call management, and error handling.
Prerequisites
- Completed
retellai-install-auth
retell-sdk installed
Instructions
Step 1: Singleton Client
import Retell from 'retell-sdk';
let _retell: Retell | null = null;
export function getRetellClient(): Retell {
if (!_retell) {
_retell = new Retell({ apiKey: process.env.RETELL_API_KEY! });
}
return _retell;
}
Step 2: Typed Agent Configuration
interface AgentConfig {
name: string;
voiceId: string;
prompt: string;
functions?: FunctionConfig[];
maxCallDurationMs?: number;
endCallAfterSilenceMs?: number;
}
async function createAgent(config: AgentConfig) {
const retell = getRetellClient();
const llm = await retell.llm.create({
model: 'gpt-4o',
general_prompt: config.prompt,
functions: config.functions,
});
const agent = await retell.agent.create({
response_engine: { type: 'retell-llm', llm_id: llm.llm_id },
voice_id: config.voiceId,
agent_name: config.name,
max_call_duration_ms: config.maxCallDurationMs || 300000,
end_call_after_silence_ms: config.endCallAfterSilenceMs || 10000,
});
return { agentId: agent.agent_id, llmId: llm.llm_id };
}