How do you call the LLM within your code
# ask-ai
r
How do you call the LLM within your code
✅ 1
t
Thanks for reaching out, Rithvik. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you’re asking how to call the LLM from your code. I’ll show the main ways you can do that and a couple of quick examples so you can pick the approach that fits your use case. Summary of options • Use an AgentSession and pass a model identifier string to the
llm
argument (quickest). • Create an
LLM
instance from the
inference
module to pass extra configuration. • Use provider plugin LLM classes (for OpenAI-compatible providers and vendors). • Use the
LLM
instance directly (streaming chat) or call
.collect()
to await a full response. • For custom behavior, override
llm_node
in your agent class. Examples 1) Quick AgentSession with model id (Python)
Copy code
from livekit.agents import AgentSession

session = AgentSession(
    llm="openai/gpt-4.1-mini"
)
(Typescript equivalent)
Copy code
const session = new AgentSession({
  llm: "openai/gpt-4.1-mini"
});
2) AgentSession with extra LLM config (Python)
Copy code
from livekit.agents import AgentSession, inference

session = AgentSession(
    llm=inference.LLM(
        model="openai/gpt-4.1-mini",
        extra_kwargs={"max_completion_tokens": 1000}
    )
)
3) Plugin-based provider (OpenAI plugin example — Python)
Copy code
from livekit.plugins import openai
from livekit.agents import AgentSession

session = AgentSession(
    llm=openai.LLM(model="gpt-4o-mini")
)
4) OpenAI-compatible providers via helper (Groq example)
Copy code
from livekit.plugins import openai

session = AgentSession(
    llm=openai.LLM.with_groq(model="llama3-8b-8192")
)
5) Use an LLM instance directly (streaming chat — Python)
Copy code
llm = openai.responses.LLM(model="gpt-4o-mini")
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="Hello!")

async with llm.chat(chat_ctx=chat_ctx) as stream:
    async for chunk in stream:
        print("Received chunk:", chunk.delta)
6) Awaiting a full response (collect)
Copy code
response = await my_llm.chat(chat_ctx=chat_ctx).collect()
print(response.text)
Custom LLM logic • If you need custom inference behavior inside an agent pipeline, override the
llm_node
method in your agent class to control how the LLM is called and how responses are handled. If you’d like, tell me: • Which language you’re using (Python or TypeScript), • Which provider/model you plan to call (example: OpenAI, Groq, Together AI), and • Whether you want streaming or a single completed response. I’ll give a tailored code snippet and next steps based on that. Sources: Large language models (LLM) overview | LiveKit Documentation | OpenAI LLM models | LiveKit Documentation | Pipeline nodes and hooks | LiveKit Documentation, with interactive elements