refined-hydrogen-81988
11/07/2025, 6:56 PMctx entrypoint(ctx: JobContext) , how to do that locally? give some basic example and advicetall-airline-16756
11/07/2025, 6:56 PMtall-airline-16756
11/07/2025, 6:57 PMctx 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:
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:
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:
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:
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:
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.pyrefined-hydrogen-81988
11/07/2025, 6:59 PMuv run src/agent.py console and pass job metadata as CLI argument. Do you have such example?tall-airline-16756
11/07/2025, 6:59 PMuv 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:
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:
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:
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