https://retellai.com/?utm_source=discord&utm_medium=seo logo
d
@AI Evy logger.info("Creating Retell LLM model..."); const newModel = await retellClient.llm.create({ model: "gpt-4o", general_prompt: RETELL_AGENT_GENERAL_PROMPT, general_tools: [ { type: "end_call", name: "end_call_1", description: "End the call if the user uses goodbye phrases such as 'bye,' 'goodbye,' or 'have a nice day.' ", }, ], }); logger.info(Created LLM model with ID: ${newModel.llm_id}); // Create retell agent const newAgent = await retellClient.agent.create({ response_engine: { llm_id: newModel.llm_id, type: "retell-llm" }, voice_id: body.voice_id, agent_name: body.name, }); logger.info(Created ${body.name} agent with ID: ${newAgent.agent_id}); I have a question about the code above: Do I need to create a new llm model every time when I need to create a retell agent?
a
Thanks for tagging me @denghuichao. I'll look through relevant sources and get back to you shortly.
### Answer No, you don't need to create a new LLM model every time you create a Retell agent [(1) ]() . LLMs persist in the system and can be reused across multiple agents . When you delete an agent, the associated LLM remains in the database and can be reused later . You can check existing LLMs using the list API to see what's available for reuse . This means you can: 1. Create LLMs once with specific configurations 2. Reuse the same LLM ID across multiple agents that need the same prompt and tool setup 3. Only create new LLMs when you need different prompts, tools, or model configurations This approach is more efficient and cost-effective than creating duplicate LLMs for each agent.
​ To learn more, see the sources I considered: [[1] Retell LLMs with no Corresponding Agent](https://discord.com/channels/1186424376300679209/1309474366018293770) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
d
Wheren can I get the created LLM id?
@AI Evy Wheren can I get the created LLM id?
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer You can get the created LLM ID in several ways: ## 1. From the Create Response When you create a Retell LLM, the response includes the
llm_id
field [(1) ]() :
Copy code
javascript
const llmResponse = await client.llm.create() ;
console.log(llmResponse.llm_id) ;
The response will contain the unique
llm_id
like
"oBeDLoLOeuAbiuaMFXRtDOLriTJ5tSxD"
. ## 2. List All LLMs You can retrieve all your existing LLMs using the list endpoint [(2) ]() :
Copy code
javascript
import Retell from 'retell-sdk';
const client = new Retell({
 apiKey: 'YOUR_RETELL_API_KEY',
}) ;
const llmResponses = await client.llm.list() ;
console.log(llmResponses) ;
This returns an array of all your LLMs, each containing their
llm_id
. ## 3. Retrieve Specific LLM If you know the LLM ID, you can retrieve its details [(3) ]() :
Copy code
javascript
import Retell from 'retell-sdk';
const client = new Retell({
 apiKey: 'YOUR_RETELL_API_KEY',
}) ;
const llmResponse = await client.llm.retrieve('16b980523634a6dc504898cda492e939') ;
console.log(llmResponse.llm_id) ;
In your code example, you're already accessing it correctly with
newModel.llm_id
after the create call [(1) ]() .
​ To learn more, see the sources I considered: [[1] Create Retell LLM](https://docs.retellai.com/api-references/create-retell-llm) [[2] List Retell LLMs](https://docs.retellai.com/api-references/list-retell-llms) [[3] Get Retell LLM](https://docs.retellai.com/api-references/get-retell-llm) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
d
@AI Evy I created a LLM on early calls some days ago, and I need to create some more agent usging the old LLM ID. How can I get thet ID ?
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer To get the ID of an LLM you created previously, you can retrieve it from an existing agent that's already using it. Here's how: ## Get LLM ID from Existing Agent If you have agents that are already using the old LLM, you can retrieve the LLM ID from the agent's response engine [(1) ]() :
Copy code
javascript
import Retell from 'retell-sdk';
const client = new Retell({
 apiKey: 'YOUR_RETELL_API_KEY',
}) ;
const agentResponse = await client.agent.retrieve('16b980523634a6dc504898cda492e939') ;
console.log(agentResponse.agent_id) ;
The response will include the
response_engine
object which contains the
llm_id
:
Copy code
javascript
{
 "response_engine": {
 "type": "retell-llm",
 "llm_id": "llm_234sdertfsdsfsdf",
 "version": 0
 }
}
You can then extract the LLM ID with
agentResponse.response_engine.llm_id
. ## List All Agents to Find LLM IDs You can also list all your agents to see which LLM IDs are being used [(2) ]() :
Copy code
javascript
import Retell from 'retell-sdk';
const client = new Retell({
 apiKey: 'YOUR_RETELL_API_KEY',
}) ;
const agentResponse = await client.agent.list() ;
This will return an array of all agents, each containing their
response_engine
with the associated
llm_id
.
​ To learn more, see the sources I considered: [[1] Get Agent](https://docs.retellai.com/api-references/get-agent) [[2] List Agents](https://docs.retellai.com/api-references/list-agents) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
d
@AI Evy That seems I can get the first one and get the ID, and if not exist, create a new on, please show me the code
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer Based on the available sources, here's how you can get an existing LLM ID from the first agent, or create a new one if no agents exist:
Copy code
javascript
import Retell from 'retell-sdk';
const client = new Retell({
  apiKey: 'YOUR_RETELL_API_KEY',
}) ;

// Try to get existing LLM ID from first agent
let llm_id;
try {
  const agentResponse = await client.agent.list() ;
  
  if (agentResponse.results && agentResponse.results.length > 0) {
    // Get LLM ID from first existing agent
    llm_id = agentResponse.results[0].response_engine.llm_id;
    console.log(`Using existing LLM ID: ${llm_id}`) ;
  } else {
    // No agents exist, create new LLM
    const newModel = await client.llm.create({
      model: "gpt-4o",
      general_prompt: RETELL_AGENT_GENERAL_PROMPT,
      general_tools: [
        {
          type: "end_call",
          name: "end_call_1",
          description: "End the call if the user uses goodbye phrases such as 'bye,' 'goodbye,' or 'have a nice day.'",
        },
      ],
    }) ;
    llm_id = newModel.llm_id;
    console.log(`Created new LLM with ID: ${llm_id}`) ;
  }
} catch (error) {
  console.error("Error getting LLM ID:", error) ;
  throw error;
}

// Now create agent with the LLM ID
const newAgent = await client.agent.create({
  response_engine: { llm_id: llm_id, type: "retell-llm" },
  voice_id: body.voice_id,
  agent_name: body.name,
}) ;
[(1) ]() This code first attempts to list existing agents . If agents exist, it extracts the
llm_id
from the first agent's
response_engine
. If no agents exist, it creates a new LLM and uses that ID for the new agent [(2) ]() .
​ To learn more, see the sources I considered: [[1] Get Agent](https://docs.retellai.com/api-references/get-agent) [[2] Create Agent](https://docs.retellai.com/api-references/create-agent) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
Thanks for the feedback, glad I was helpful.
d
@AI Evy let llm_id; const agentResponse = await retellClient.agent.list(); if (agentResponse && agentResponse.length > 0) { // Get LLM ID from first existing agent llm_id = agentResponse[0].response_engine.llm_id; console.log(
Using existing LLM ID: ${llm_id}
); } Property 'llm_id' does not exist on type 'ResponseEngineRetellLm | ResponseEngineCustomLm | ResponseEngineConversationFlow'. Property 'llm_id' does not exist on type 'ResponseEngineCustomLm'.
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer The TypeScript error occurs because the
response_engine
can be one of multiple types, and not all types have the
llm_id
property [(1) ]() . You need to check the type first before accessing
llm_id
. Here's the corrected code that handles the different response engine types :
Copy code
javascript
let llm_id;
const agentResponse = await retellClient.agent.list() ;
if (agentResponse && agentResponse.length > 0) {
    const responseEngine = agentResponse[0].response_engine;
    
    // Check if it's a retell-llm type before accessing llm_id
    if (responseEngine.type === "retell-llm") {
        llm_id = responseEngine.llm_id;
        console.log(`Using existing LLM ID: ${llm_id}`) ;
    } else {
        console.log("First agent doesn't use retell-llm, creating new LLM") ;
        // Create new LLM logic here
    }
}
The response engine can be one of three types according to the documentation : -
retell-llm
(which has
llm_id
) -
custom-llm
(which doesn't have
llm_id
) -
conversation-flow
(which doesn't have
llm_id
) By checking
responseEngine.type === "retell-llm"
first, TypeScript will know that the object has the
llm_id
property .
​ To learn more, see the sources I considered: [[1] List Agents](https://docs.retellai.com/api-references/list-agents) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
d
get the first agentResponse witch responseEngine.type === "retell-llm" @AI Evy
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer Here's how to get the first agent response where `responseEngine.type === "retell-llm"`:
Copy code
javascript
import Retell from 'retell-sdk';
const client = new Retell({
 apiKey: 'YOUR_RETELL_API_KEY',
}) ;
const agentResponses = await client.agent.list() ;
[(1) ]() Then filter for the first agent with a retell-llm response engine:
Copy code
javascript
let llm_id;
const agentResponse = await retellClient.agent.list() ;

if (agentResponse && agentResponse.length > 0) {
    // Find the first agent with retell-llm response engine
    const retellLlmAgent = agentResponse.find(agent => 
        agent.response_engine.type === "retell-llm"
    ) ;
    
    if (retellLlmAgent) {
        llm_id = retellLlmAgent.response_engine.llm_id;
        console.log(`Using existing LLM ID: ${llm_id}`) ;
    } else {
        console.log("No agents with retell-llm found, creating new LLM") ;
        // Create new LLM logic here
    }
}
The response structure shows that agents can have different response engine types, and only the
retell-llm
type contains the
llm_id
field . The
find()
method will return the first agent that matches the condition, or
undefined
if none are found.
​ To learn more, see the sources I considered: [[1] List Agents](https://docs.retellai.com/api-references/list-agents) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
d
@AI Evy directlly check type and cast ResponseEngineRetellLm
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer Here's how to directly check the type and cast to `ResponseEngineRetellLm`:
Copy code
javascript
let llm_id;
const agentResponse = await retellClient.agent.list() ;

if (agentResponse && agentResponse.length > 0) {
    // Find the first agent with retell-llm response engine
    const retellLlmAgent = agentResponse.find(agent => 
        agent.response_engine.type === "retell-llm"
    ) ;
    
    if (retellLlmAgent) {
        // Type assertion/cast to ResponseEngineRetellLm
        const retellLlmEngine = retellLlmAgent.response_engine as ResponseEngineRetellLm;
        llm_id = retellLlmEngine.llm_id;
        console.log(`Using existing LLM ID: ${llm_id}`) ;
    } else {
        console.log("No agents with retell-llm found, creating new LLM") ;
        // Create new LLM logic here
    }
}
[(1) ]() Alternatively, you can use a type guard approach:
Copy code
javascript
// Type guard function
function isRetellLlmEngine(engine: any) : engine is ResponseEngineRetellLm {
    return engine.type === "retell-llm";
}

let llm_id;
const agentResponse = await retellClient.agent.list() ;

if (agentResponse && agentResponse.length > 0) {
    const firstAgent = agentResponse[0];
    
    if (isRetellLlmEngine(firstAgent.response_engine) ) {
        // TypeScript now knows this is ResponseEngineRetellLm
        llm_id = firstAgent.response_engine.llm_id;
        console.log(`Using existing LLM ID: ${llm_id}`) ;
    }
}
Both approaches will resolve the TypeScript error by ensuring you're working with the correct response engine type that contains the
llm_id
property.
​ To learn more, see the sources I considered: [[1] List Agents](https://docs.retellai.com/api-references/list-agents) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
d
@AI Evy import ResponseEngineRetellLm
a
Thanks for the follow-up, @denghuichao. I'll get back to you soon.
### Answer To import
ResponseEngineRetellLm
, you'll need to import it from the Retell SDK types [(1) ]() :
Copy code
typescript
import Retell, { ResponseEngineRetellLm } from 'retell-sdk';
The TypeScript SDK provides access to all the request and response types, including
ResponseEngineRetellLm
. You can then use it for type casting as discussed in your previous message:
Copy code
typescript
const retellLlmEngine = retellLlmAgent.response_engine as ResponseEngineRetellLm;
llm_id = retellLlmEngine.llm_id;
This approach gives you proper TypeScript type safety when working with the response engine that contains the
llm_id
property .
​ To learn more, see the sources I considered: [[1] Retell Node API Library](https://github.com/RetellAI/retell-typescript-sdk/blob/main/README.md) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.