give me some good practices for local development....
# ask-ai
r
give me some good practices for local development. let's say I want to pass some metadata in
ctx
entrypoint(ctx: JobContext)
, how to do that locally? give some basic example and advice
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to share some helpful best practices and examples for local development when you're passing metadata in
ctx
with
entrypoint(ctx: JobContext)
. These tips can make your work smoother and more effective: • Use Job Metadata: Try passing structured data like JSON in
ctx.job.metadata
. For instance, to personalize a greeting, you might do something like this:
Copy code
import json

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

    initial_ctx = ChatContext()
    initial_ctx.add_message(role="assistant", content=f"The user's name is {user_name}.")

    session = AgentSession()
    await session.start(room=ctx.room, agent=Assistant(chat_ctx=initial_ctx))
    await session.generate_reply(instructions=f"Greet {user_name} by name.")
• Parse Participant Metadata: It’s a good idea to safely parse JSON from
participant.metadata
and include error handling like this:
Copy code
async def entrypoint(ctx: JobContext):
    participant = await ctx.wait_for_participant()
    try:
        metadata = json.loads(participant.metadata) if participant.metadata else {}
    except json.JSONDecodeError:
        metadata = {}
    user_name = metadata.get("user_name", "User")
• Initialize Agent Session: Preloading context before starting the session can really help keep things organized:
Copy code
initial_ctx = ChatContext()
initial_ctx.add_message(role="user", content="Hello!")
session = AgentSession()
await session.start(room=ctx.room, agent=Assistant(chat_ctx=initial_ctx))
• Use Room and Participant Attributes: Access metadata dynamically to make your code more flexible:
Copy code
room_metadata = ctx.room.metadata
for p in ctx.room.participants.values():
    role = p.attributes.get("role", "guest")
• Enhance Logs: Adding custom fields can really improve observability and debugging:
Copy code
ctx.log_context_fields = {"room_name": ctx.room.name, "user_id": metadata.get("user_id", "unknown")}
I hope these practices help you build a robust and maintainable local development setup. If you’d like me to walk you through any part of this or provide more examples, just let me know—I’m here to help! Sources: Job lifecycle | LiveKit Docs | External data and RAG | LiveKit docs | agent/main.py | Vision | LiveKit Docs | livekit-agents/livekit/agents/job.py
r
I want to run my agent locally with
uv run src/agent.py console
and pass job metadata as CLI argument. Do you have such example?
t
Hi there! I wanted to share some helpful info about the
uv run
command. It doesn’t support passing job metadata directly as a CLI argument, but don’t worry—we have a few great ways to handle this during the dispatch phase or by embedding metadata in tokens. Here are some options for you: • Pass Metadata During Dispatch: You can use the
lk dispatch create
command with the
--metadata
flag like this:
Copy code
lk dispatch create \
  --agent-name test-agent \
  --room my-room \
  --metadata '{"user_id": "12345", "user_name": "Alice"}'
• Embed Metadata in Tokens: Another way is to include metadata when creating tokens using the
--job-metadata
flag:
Copy code
lk token create \
  --identity user123 \
  --join \
  --room my-room \
  --agent test-agent \
  --job-metadata '{"user_id": "123", "context": "support_call"}'
• Simulate Metadata Locally: If you want to test things locally, you can use environment variables or mock data in your script like this:
Copy code
import os
import json

async def entrypoint(ctx: JobContext):
    if not ctx.job.metadata:
        env_meta = os.getenv("JOB_METADATA")
        if env_meta:
            ctx.job.metadata = json.loads(env_meta)
    user_id = ctx.job.metadata.get("user_id")
    print(f"Using metadata: user_id={user_id}")
If any of this feels unclear or if you’d like a hand setting up your local environment, just let me know—I’m here to help! Sources: pkg/agentfs/examples/python.uv.Dockerfile | Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs | README.md | examples/agent_dispatch.py