Does chat context manages all the chats of room un...
# ask-ai
a
Does chat context manages all the chats of room until the call ends?
t
Thanks for reaching out, giri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Chat context in LiveKit manages chat messages during an active room session, but it does not persist them until the call ends. Messages are transmitted in real-time between connected participants and are maintained in memory only during the active session. Once the room session ends or participants disconnect, the messages are cleared. If you need to retain chat history beyond the session duration, you will need to implement your own storage solution using a database or other persistence layer.
a
How to access data in chat context and what fields are maintained by defaulg
t
Hey there! Let me walk you through how you can easily access data in LiveKit's chat context. It's pretty straightforward, and I'll break it down in a friendly way. Accessing Data: • JavaScript/TypeScript: Think of the
ChatContext
like your chat's personal assistant. You can grab all your messages and even add new ones super easily. Check this out:
Copy code
const chatContext = new ChatContext();
const allMessages = chatContext.messages;
chatContext.append({ text: "Hello", role: ChatRole.USER });
• Python: Same idea here - just a simple way to manage your chat data. It's like having a neat little chat organizer:
Copy code
chat_ctx = ChatContext()
all_items = chat_ctx.items
chat_ctx.add_message(role="user", content="Hello")
Default Fields Maintained: • Core Protocol: Fields include
id
,
timestamp
,
message
,
editTimestamp
,
deleted
, and
generated
. • JavaScript SDK: Additional fields include
role
,
name
,
content
,
toolCalls
,
toolCallId
, and
toolException
. • Python SDK: Fields include
id
,
type
,
role
,
content
,
interrupted
,
transcript_confidence
,
hash
, and
created_at
. • Flutter SDK: Fields include
message
,
timestamp
,
id
,
sender
, and
participant
. Don't worry about the details - LiveKit's got your back and automatically handles all these fields for you behind the scenes! Sources: agents/src/llm/chat_context.ts | livekit-agents/livekit/agents/llm/chat_context.py | External data and RAG | LiveKit Docs | packages/livekit-rtc/src/proto/room_pb.ts | lib/src/context/chat_context.dart
a
Is it correct run_context.agent.chat.chat_ctx?
t
Hey there! Let me help you understand how to access the chat context in LiveKit. It's actually pretty straightforward depending on which SDK you're using: • Python: If you're working with Python, you'll want to use the
Agent
class's
chat_ctx
property. It's super easy – just type
agent.chat_ctx
and you'll get a read-only view of your current chat context. • JavaScript/TypeScript: For JavaScript or TypeScript fans, the
VoicePipelineAgent
and
LLMStream
classes have a handy
chatCtx
accessor. Just grab it with
agent.chatCtx
. I noticed you might have been trying to use
run_context.agent.chat.chat_ctx
, but that doesn't quite match up with the documented ways of accessing the chat context. No worries though – stick with the methods I mentioned, and you'll be good to go! Sources: livekit-agents/livekit/agents/voice/audio_recognition.py | VoicePipelineAgent | LiveKit Agents | LLMStream | LiveKit Agents
a
How to use it agent.chat_ctx import statement
t
Hey there! Let me walk you through importing
agent.chat_ctx
in LiveKit – it's actually pretty simple. 😊 • Basic Import for Agent:
Copy code
from livekit.agents import Agent
• If you also need ChatContext directly:
Copy code
from livekit.agents import Agent, ChatContext
• Alternative Import Using the llm Module:
Copy code
from livekit.agents import Agent, llm
# Then use llm.ChatContext
• Or Import ChatContext from the llm Submodule:
Copy code
from livekit.agents import Agent
from livekit.agents.llm import ChatContext
Quick tip:
agent.chat_ctx
gives you a read-only peek into the agent's current chat context as an
llm.ChatContext
object. Cool, right? Here's a quick example to help you get started:
Copy code
from livekit.agents import Agent

# Create an agent instance
agent = Agent(instructions="You are a helpful assistant.")

# Access the chat context
chat_context = agent.chat_ctx
Pro tip: The most straightforward way is just using
from livekit.agents import Agent
, which lets you easily access the
chat_ctx
property on your agent. Happy coding! 🚀 Sources: livekit-agents/livekit/agents/voice/agent_activity.py
a
I don't want to create agent would be dispatch using dispatcheragent but it should be available in other file
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!