steep-balloon-41261
09/05/2025, 5:38 PMtall-airline-16756
09/05/2025, 5:38 PMtall-airline-16756
09/05/2025, 5:38 PMpowerful-dog-59275
09/05/2025, 5:54 PMtall-airline-16756
09/05/2025, 5:55 PMbefore_llm_cb Callback: This lets you trim down the chat context before sending it to the LLM. For instance, you might want to keep just the last 15 messages to save on tokens:
async def truncate_context(assistant: VoicePipelineAgent, chat_ctx: llm.ChatContext):
if len(chat_ctx.messages) > 15:
chat_ctx.messages = chat_ctx.messages[-15:]
• Manage Instructions Explicitly: When you use generateReply, the instructions parameter is great for temporary instructions that don’t get saved in the chat history. Try not to add to instructions unnecessarily to keep things lean.
• Update Instructions Only When Needed: Use update_instructions() to change agent instructions during runtime, but make sure you replace old instructions instead of piling them up.
• Leverage Automatic Context Truncation: Some models automatically cut down the context once it goes beyond a certain number of messages, like 40. You can also add similar trimming in your app.
• Avoid Redundant Context Updates: Use user_input for ongoing conversation bits and keep instructions for one-off directions to avoid repeating yourself.
Following these tips will help you keep token use efficient and boost performance during longer sessions. If you want to chat more about this or need a hand, just let me know!
Sources: plugins/google/src/beta/realtime/realtime_api.ts | Agent speech and audio | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py