busy-monitor-72069
09/27/2025, 6:45 AMtall-airline-16756
09/27/2025, 6:45 AMtall-airline-16756
09/27/2025, 6:46 AMllm_node in a custom agent to capture the output from the first LLM.
2. Take that output to build a new ChatContext for the second LLM.
3. Stream the second LLM's output directly to the TTS node for synthesis.
This does involve some custom coding since LiveKit doesn’t have built-in support for chaining multiple LLMs in sequence. But the great news is that the framework offers the essential building blocks, like overridable pipeline nodes and stream manipulation tools, to help you put this together smoothly.
If you'd like, I can provide detailed guidance on how to implement this, including code examples or support for features like chunk-by-chunk streaming. Just let me know how I can best assist you!
Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | chain-of-thought-tts/agent.py | docs/index.yamlbusy-monitor-72069
09/27/2025, 6:48 AMtall-airline-16756
09/27/2025, 6:48 AMfrom livekit.agents import VoicePipelineAgent, llm
from livekit.plugins import openai
from typing import AsyncIterable
class SequentialLLMAgent(VoicePipelineAgent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Initialize the second LLM (e.g., GPT-4)
self.second_llm = openai.LLM(model="gpt-4")
async def llm_node(
self,
chat_ctx: llm.ChatContext,
tools: list[llm.FunctionTool | llm.RawFunctionTool],
model_settings: llm.ModelSettings,
) -> AsyncIterable[llm.ChatChunk | str]:
# Step 1: Run the first LLM
first_llm_stream = await super().llm_node(chat_ctx, tools, model_settings)
# Step 2: Collect the full output from the first LLM
first_output = ""
async for chunk in first_llm_stream:
if isinstance(chunk, str):
first_output += chunk
elif isinstance(chunk, llm.ChatChunk) and chunk.delta and chunk.delta.content:
first_output += chunk.delta.content
# Step 3: Use the first LLM's output as input to the second LLM
second_chat_ctx = llm.ChatContext().append(
role="user",
text=f"Refine or elaborate on this response: {first_output}"
)
second_tools = [] # Define tools if needed
second_model_settings = llm.ModelSettings()
# Call the second LLM
second_llm_stream = await self.second_llm.chat(
chat_ctx=second_chat_ctx,
tools=second_tools,
tool_choice=second_model_settings.tool_choice,
conn_options=None,
)
# Step 4: Stream the second LLM's output
async for chunk in second_llm_stream:
yield chunk
# Instantiate the agent
agent = SequentialLLMAgent(
vad=openai.VAD(),
stt=openai.STT(),
llm=openai.LLM(model="gpt-3.5-turbo"), # First LLM
tts=openai.TTS(),
)
This agent will:
• Use the first LLM (like GPT-3.5) to generate an initial response.
• Then, it takes that response and passes it on as input to the second LLM (like GPT-4) for refinement.
• Finally, it streams the polished response to TTS for synthesis.
If you have any questions or would like help adding features like chunk-by-chunk streaming, please don’t hesitate to ask. I’m here to help!