how can i initialize my agent with data or context...
# ask-ai
w
how can i initialize my agent with data or context upon connection?
t
Thanks for reaching out, Nathan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you get started with initializing your agent using data or context when it connects. Here are a few friendly ways you can do this: • Job Metadata: You can pass structured data, like JSON, through job metadata right at startup. This way, your agent has the important context it needs before it begins interacting. • UserData Class: Consider using a Python dataclass to keep track of session-level state and share data throughout the agent's lifecycle. It’s a neat way to stay organized! • Agent Constructor: Another option is to pass static or simple dynamic context directly into the agent’s constructor. It’s straightforward and effective. • Room or Participant Attributes: You can also set context based on attributes assigned to the room or participants, which helps tailor interactions nicely. • Dynamic Updates: Lastly, you can enrich the context dynamically during the conversation by using event handlers like
on_user_turn_completed
. This keeps things flexible and responsive. If you’d like, I’d be more than happy to walk you through detailed examples or help you implement any of these methods. Just let me know! Sources: Agent dispatch | LiveKit Docs | External data and RAG | LiveKit Docs | telephony/survey_caller/survey_calling_agent.py | Building an Automated IVR Menu Caller | LiveKit Docs
w
can you show me an example of passing job metadata at startup
t
I'd love to walk you through an example of how to pass job metadata at startup to initialize your agent:
Copy code
import json
from livekit import JobContext, AgentSession, ChatContext, Assistant

async def entrypoint(ctx: JobContext):
    # Parse job metadata
    metadata = json.loads(ctx.job.metadata)
    user_name = metadata["user_name"]
    city = metadata["city"]

    # Create initial chat context
    initial_ctx = ChatContext()
    initial_ctx.add_message(
        role="assistant",
        content=f"The user's name is {user_name} and they live in {city}."
    )

    # Start the agent session
    session = AgentSession()
    await session.start(
        room=ctx.room,
        agent=Assistant(chat_ctx=initial_ctx)
    )
Here's what's happening in this example: • The
ctx.job.metadata
holds the metadata provided at job startup, usually in JSON format. • We parse this metadata to pull out user details like
user_name
and
city
. • Then, we create an initial chat context and add a message that includes this extracted information. • Finally, the agent session is started using the initialized context. If anything feels unclear or you'd like me to share more examples, please don't hesitate to ask—I'm here to help!
w
how do you pass the metadata at startup from the client making the connection? ios for example
t
To pass job metadata at startup from an iOS client, you can include the metadata in the connection configuration. Here's an example:
Copy code
import LiveKit

var room = Room()
var config = ConnectConfig()
config.metadata = "{\"user_id\": \"123\", \"user_name\": \"John Doe\"}" // JSON string

room.connect("wss://your-server-url", token: "your-token", config: config) { result in
    // Handle connection result
}
In this example: • The
metadata
field is set in the
ConnectConfig
object as a JSON string. • This metadata is passed to the server when the client connects, and it can be accessed in the agent using
ctx.job.metadata
. If you have any questions or need a hand with implementing this, please don’t hesitate to ask—I’m here to help! Sources: Room metadata | LiveKit Docs | Participant attributes and metadata | LiveKit docs | cmd/lk/token.go | Sources/LiveKit/Protos/livekit_rtc.pb.swift | Agent dispatch | LiveKit Docs