flaky-scooter-85113
09/23/2025, 5:07 PMtall-airline-16756
09/23/2025, 5:07 PMtall-airline-16756
09/23/2025, 5:08 PMsession.history to grab all the messages exchanged during your session.
• Generate summary at session end: Try using the add_shutdown_callback method to run a function that processes the history and creates a summary right when the session closes.
Here’s a little example to guide you:
// Pseudocode for agent-js
const chatHistory = session.history.items;
const summaryText = chatHistory.map(msg =>
`${msg.role}: ${msg.text_content}`
).join('\n');
// Optionally send summary via TTS or log it
await agent.say(`Summary of our conversation: ${summaryText}`);
You can also listen for conversation_item_added events as they happen to build your transcript bit by bit.
If you’re looking to create a metrics summary (like API usage), the UsageCollector class from the metrics module is really handy:
const usageCollector = new metrics.UsageCollector();
session.on('metrics_collected', (ev) => {
usageCollector.collect(ev.metrics);
});
ctx.addShutdownCallback(async () => {
const summary = usageCollector.getSummary();
console.log('Usage Summary:', summary);
});
Feel free to reach out if you’d like me to walk you through anything else or if you have more questions—I’m here to help!
Sources: Session recording and transcripts | LiveKit Docs | Session recording and transcripts | LiveKit Docs | Agent-assisted warm transfer | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docsflaky-scooter-85113
09/23/2025, 5:09 PMtall-airline-16756
09/23/2025, 5:10 PMgpt-4o-mini) and include your API key:
import * as openai from '@livekit/agents-plugin-openai';
const llmInstance = new openai.LLM({
model: 'gpt-4o-mini',
apiKey: process.env.OPENAI_API_KEY,
});
1. Access and Filter Conversation History: Next, use session.chat_ctx.copy() to grab and filter your conversation history, making sure to exclude any non-essential messages for a cleaner summary:
const ctxCopy = session.chat_ctx.copy({
excludeEmptyMessage: true,
excludeInstructions: true,
excludeFunctionCall: true,
});
1. Format Messages for the LLM: Now, prepare the conversation history as a string that the LLM can easily understand for summarization:
let prevConvo = "";
for (const msg of ctxCopy.items) {
if (msg.role === "user") {
prevConvo += `Customer: ${msg.text_content}\n`;
} else {
prevConvo += `Assistant: ${msg.text_content}\n`;
}
}
1. Generate the Summary: Finally, use the LLM to create a summary of the conversation:
const chatCtx = new llm.ChatContext();
chatCtx.addMessage({
role: 'user',
content: `Summarize the following conversation:\n${prevConvo}`,
});
const stream = llmInstance.chat({ chatCtx });
for await (const chunk of stream) {
console.log(chunk?.text || '');
}
This method should help you get a clear and concise summary using the OpenAI RealTime model. If anything feels unclear or if you’d like me to walk you through any part again, just let me know—I’m here to help!
Sources: Agent-assisted warm transfer | LiveKit Docs | Large language model (LLM) integrations | LiveKit Docs | examples/voice_agents/realtime_load_chat_history.py | OpenAI Realtime API integration guide | LiveKit Docs | OpenAI LLM integration guide | LiveKit Docsflaky-scooter-85113
09/23/2025, 5:17 PMtall-airline-16756
09/23/2025, 5:17 PMllmInstance is basically your connection to the OpenAI Language Model that you set up in your code to chat with the OpenAI API. It’s created using the OpenAI plugin for LiveKit Agents. Here’s a simple way to define it:
import * as openai from '@livekit/agents-plugin-openai';
const llmInstance = new openai.LLM({
model: 'gpt-4o-mini', // Choose the model you want to work with
apiKey: process.env.OPENAI_API_KEY, // Your OpenAI API key goes here
});
This instance lets you send requests to the OpenAI model, like summarizing a chat or generating helpful responses. If anything’s unclear or you want me to walk you through it more, just let me know—I’m here to help!flaky-scooter-85113
09/23/2025, 5:24 PMfor await (const chunk of stream) {
console.log(chunk.text || '');
}
it says: .text does not exist on chunk.tall-airline-16756
09/23/2025, 5:25 PMchunk object in the OpenAI LLM chat stream doesn’t have a direct .text property. Instead, you’ll want to access the content using chunk.delta.content. Here’s a little tweak to your code that should do the trick:
for await (const chunk of stream) {
if (chunk.delta && chunk.delta.content) {
console.log(chunk.delta.content);
}
}
This way, you can safely grab the content from each chunk without running into errors. If anything’s unclear or you’d like me to walk you through it, just let me know—I’m here to help!
Sources: plugins/openai/src/realtime/realtime_model.ts | livekit-plugins/livekit-plugins-anthropic/livekit/plugins/anthropic/llm.py | pipeline-llm/llm_powered_content_filter.py