How do I make an LLM request in livekit? ```analys...
# ask-ai
c
How do I make an LLM request in livekit?
Copy code
analysis_llm = openai.LLM(
        model="gpt-4.1-mini"
    )
t
Thanks for reaching out, Anton. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to guide you through making an LLM request in LiveKit! Here’s a simple step-by-step approach: 1. Initialize the LLM: Start by using the
LLM
class from the LiveKit Agents framework. You'll want to specify the model, API key, and secret. Here's an example to get you going:
Copy code
from livekit.agents.inference.llm import LLM

llm_instance = LLM(
    model="gpt-4.1",
    api_key="your_api_key",
    api_secret="your_api_secret"
)
1. Prepare the Chat Context: Next, use
ChatContext
to build your conversation history or prompt. This helps the model understand the context of the chat:
Copy code
from livekit.agents.llm import ChatContext

chat_ctx = ChatContext().append(text="Hello, how can I help you?", role="user")
1. Make the Request: Now, call the
chat()
method on your LLM instance, passing in the
ChatContext
you prepared:
Copy code
stream = llm_instance.chat(chat_ctx=chat_ctx)
You can then iterate over the response stream asynchronously to process the output as it comes in, like this:
Copy code
async for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content)
If you have any questions or need a hand with any of these steps, please don’t hesitate to ask. I’m here to help! Sources: tests/test_llm.py | Baseten LLM integration guide | LiveKit Docs | Azure OpenAI LLM integration guide | LiveKit Docs | Amazon Bedrock LLM integration guide | LiveKit Docs | Ollama integration guide | LiveKit Docs
c
Is there a non-stereaming mode?
t
Absolutely, LiveKit does support non-streaming mode for LLM requests! While streaming is the default because it’s great for real-time interactions, you can definitely handle responses in a non-streaming (batch) way by waiting for the entire response before moving forward. Here’s how you can set up non-streaming mode: 1. Use the
LLM.chat()
method and await the full response instead of processing it bit by bit. 2. If needed, override the
llm_node
method to return a complete string rather than a stream. 3. All the LLM providers we support (like OpenAI and Google Gemini) offer both streaming and non-streaming options, so you can tailor it to what works best for you. If you’d like, I’m happy to share more details or some examples to help you get started! Sources: Docs::Agents | JavaScript Agents & LLM Integration | livekit/livekit_composite | DeepWiki | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/llm.py | chain-of-thought-tts/README.mdoc | Large language model (LLM) integrations | LiveKit Docs
c
How do I constrain the response format to JSON?
t
To help you constrain the LLM response format to JSON in LiveKit, you can use the
response_format
parameter when calling the LLM. Let me walk you through the steps: 1. Define a Schema: You can use
TypedDict
or
Pydantic
to set up the expected JSON structure. For example:
Copy code
from typing_extensions import TypedDict

class ResponseSchema(TypedDict):
    action: str
    response: str
1. Pass the Schema: When you call
llm.chat()
, just include the schema in the
response_format
parameter like this:
Copy code
async with llm.chat(
    chat_ctx=chat_ctx,
    response_format=ResponseSchema
) as stream:
    async for chunk in stream:
        print(chunk)
1. Validate JSON: If you’re working with models that don’t have native JSON support, a good approach is to combine strict prompting with some post-processing to make sure the JSON output is valid. If you’d like, I’m here to provide more examples or help you step-by-step—just let me know! Sources: Tool definition and use | LiveKit Docs | examples/voice_agents/structured_output.py | Docs::Agents | tools/server/server.cpp | meditation_assistant/assistant_functions.py