What is the convenient way to generate and log cal...
# ask-ai
f
What is the convenient way to generate and log call summary in the Python AgentSession after user is disconnected?
t
Thanks for reaching out, Taras. 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 generating and logging a call summary in a Python
AgentSession
after the user disconnects. Here's a simple way to approach it: 1. Capture Chat History: You can use the
on("close")
event handler to log the session's chat history. Just access the history via
session.history.items
and log messages, function calls, and outputs in a structured way. 2. Aggregate Metrics: Consider using
metrics.UsageCollector
to gather and log performance metrics. You can register a shutdown callback with
ctx.add_shutdown_callback()
to neatly log the aggregated summary when the session ends. 3. Enrich Logs: It’s helpful to include contextual data like the room name or participant metadata. You can store this during session setup and then access it in the close handler to make your logs more informative. 4. Optional Cleanup: Depending on what you need, you might want to delete the room or disconnect the agent after logging. This step is optional and based on your use case. If you'd like, I can provide detailed code examples or help you with any part of this process—just let me know! Sources: tests/test_agent_session.py | main.py | SIP participant | LiveKit Docs | Job lifecycle | LiveKit Docs
f
How do I make my LLM to generate short summary based on the session.history.items?
t
I'd love to help you better! Could you please share which LLM or method you're using to generate the summary? For instance, are you working with a particular API or library to interact with the LLM? This will help me guide you more effectively.
f
google.LLM(model="gemini-2.5-flash")
t
I'd love to help you generate a short summary using the
google.LLM(model="gemini-2.5-flash")
. Here’s a simple way to do it step-by-step: 1. Manage Chat History: If you don’t have direct access to
session.history.items
, no worries! You can manually keep track of the conversation history during your session within your application logic. 2. Build a ChatContext: Next, create a
llm.ChatContext
object and add the conversation history to it. For example:
Copy code
chat_ctx = llm.ChatContext()
for msg in session_history:
    chat_ctx.add_message(role=msg["role"], content=msg["content"])

chat_ctx.add_message(
    role="system",
    content="Summarize the key points of the conversation above."
)
1. Generate the Summary: Now, use the Gemini model to process this context and generate your summary. Here’s how you might do it:
Copy code
gemini_llm = google.LLM(model="google/gemini-2.5-flash")

async def summarize():
    stream = gemini_llm.chat(chat_ctx=chat_ctx)
    summary = ""
    async for chunk in stream:
        if chunk.type == llm.ChatChunk.ChunkType.CONTENT:
            summary += chunk.delta.content or ""
    return summary
If
session.history.items
comes from a custom setup or a specific API you’re working with, just let me know! I’m here to help tailor the guidance to fit your needs perfectly. Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | realtime/gemini_live_vision.py | Gemini Live API plugin | LiveKit Docs | Google Gemini LLM | LiveKit Docs | Large language models (LLM) | LiveKit Docs
f
RuntimeError: no activity context found, the agent is not running
t
It looks like you're encountering the error
RuntimeError: no activity context found, the agent is not running
. This usually happens when methods that need an active runtime context, like
update_instructions
or
update_chat_ctx
, are called outside an active
AgentSession
. Let’s walk through some steps to help you resolve this: 1. Start the AgentSession: Make sure your agent is started within an
AgentSession
by using
await session.start(agent)
. This sets up the right context. 2. Call Updates During Active Session: Try to call methods like
update_instructions
or
update_tools
only after your session has started and before it ends. 3. Set Initial Configuration: If you don’t have an active session yet, you can configure your agent during initialization by providing
instructions
,
chat_ctx
, and
tools
right in the constructor. 4. Avoid Global Scope Calls: It’s best not to call runtime-dependent methods in the global scope or outside an async context, as that can lead to this error. If you’d like, feel free to share more details or your specific code snippet causing this issue—I’m here to help you get this sorted out! Sources: Still Have Questions | livekit-agents/livekit/agents/voice/agent_activity.py | Agent Session & Activity Management | livekit/livekit_composite | DeepWiki
f
Copy code
async def on_enter(self):
        logger.info(f"on_enter | {self.__class__.__name__} agent activated", extra=self.log_extras)

        @self.session.on("close")
        def on_close(ev: CloseEvent):
            logger.info(f"session.on | close | {ev.reason=}, {ev.error=}", extra=self.log_extras)
            print(self.session.history.items)
t
Thank you for reaching out! I noticed that the
session.history.items
property isn't documented in our knowledge base. Could you please let me know if this is something custom you've implemented? Any additional details about how you're managing session history would be really helpful. I'm here to assist you every step of the way!