how can i make asynchronous api calls to the llms ...
# ask-ai
p
how can i make asynchronous api calls to the llms in livekit? i want to reduce latency and ttft. will that effectively help?
t
Thanks for reaching out, Vaneeza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring ways to make asynchronous API calls to LLMs in LiveKit to reduce latency and TTFT! One helpful approach is using the
LLMStream
interface. This lets you stream responses as they’re generated, so you can process tokens incrementally. When you call the
chat()
method on an LLM instance, it returns an
LLMStream
object that you can asynchronously iterate over to handle responses smoothly. For instance, by using the
LLMStream.__aiter__()
method, you can process each token right as it becomes available. Plus, running tasks like tool calls concurrently with
asyncio.gather()
helps keep everything running without blocking. To make things even better and reduce TTFT and latency, it’s a great idea to deploy your agent server close to the LLM, STT, and TTS services. Also, don’t forget to take advantage of the LiveKit Agents Metrics API to keep an eye on performance metrics like TTFT and end-to-end latency, so you can continuously optimize your setup. Sources: livekit-plugins/livekit-plugins-lmnt/livekit/plugins/lmnt/tts.py | How can I reduce latency in voice agents using STT, TTS and LLM? | README.md | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | Troubleshooting & Common Issues | livekit/livekit_composite | DeepWiki
p
do agent class and agent session handle this by default? or do i need any specific changes in code?
t
Hi there! Just to let you know, the Agent class and AgentSession in LiveKit are designed to handle asynchronous LLM calls right out of the box, so you usually don’t need to change any code for typical use. The framework thoughtfully manages async operations for you, including streaming responses and handling multiple tasks at once. That said, if you’re customizing things by overriding methods like
llm_node
,
stt_node
, or
tts_node
, or building your own pipeline logic, you’ll want to take care of async streams yourself. For instance, in a custom
llm_node
, you’d handle async iteration to process responses like this:
Copy code
async def llm_node(self, chat_ctx, tools, model_settings):
    async for chunk in custom_llm_stream:
        yield chunk
And if you’re diving into advanced scenarios like integrating custom tools or managing speech tasks, you might also work with context variables or task creation utilities. Please feel free to reach out if you’d like me to walk you through any specific customizations — I’m here to help! Sources: agents/src/voice/agent_activity.ts | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/agent_session.py
p
how can i implement prompt chunking within livekit t reduce latency? Optimize Prompt Length and Chunking Problem: Longer prompts take more time to process, especially when passed over to large models via cloud APIs. While the prompt optimization might be on the user’s end, certain strategies can help reduce the overall latency. Solution: • Shorten Prompt Length: If you control how the prompt is structured, reduce its length by eliminating redundant information or using shorter tokens. This helps the LLM process the input faster. • Chunking: If the prompt is too long, split it into smaller chunks and process them sequentially or in parallel. This reduces the latency per chunk, allowing the system to respond quicker as chunks are processed.
t
To help you implement prompt chunking in LiveKit and reduce latency, here are some friendly strategies you might find useful: • Streaming LLM Responses: LiveKit supports streaming LLM responses, which lets you process tokens bit by bit. You can asynchronously iterate over the response stream and handle chunks as they come in—this can really help speed things up! • Sentence-Based Chunking: A great way to create natural chunks is by detecting sentence boundaries, like periods. This way, TTS processing can start even before the full response is ready. For example:
Copy code
if strings.HasSuffix(strings.TrimSpace(delta), ".") {
    return sb.String(), nil
}
• System Prompt Optimization: Try structuring your prompts to encourage concise, chunk-friendly responses. Adding instructions like "Make concise/short answers" in the system prompt can really make a difference. • Custom TTS Node: If you want more control, you can override the default TTS node to add your own chunking logic, like splitting beyond sentence boundaries or handling special content types. • UTF-8 Aware Chunking: Don’t forget to use UTF-8 aware chunking tools to properly handle multilingual content or special characters—it ensures everything runs smoothly. • Performance Monitoring: Keep an eye on how your chunking performs by using logging and metrics. This helps you fine-tune things and get the best latency possible. If you’d like, I’m here to help you dive into any specific implementation details or customize the chunking logic to fit your particular needs. Just let me know!