big-school-62939
12/16/2025, 5:46 PMtall-airline-16756
12/16/2025, 5:46 PMtall-airline-16756
12/16/2025, 5:47 PMctx.job.metadata), provide it when dispatching the job (e.g., lk dispatch create --metadata '...').
• For local testing with uv run, simulate metadata by using environment variables or command-line arguments and load those into your agent at startup.
Options
1) Dispatch with metadata (makes ctx.job.metadata available)
• Use the LiveKit CLI when creating a dispatch:
lk dispatch create --agent-name my-agent --room test-room --metadata '{"user_name":"Alice","user_id":"12345"}'
• In your agent entrypoint, read it via ctx.job.metadata (parse JSON).
2) Simulate metadata when running locally with uv run
• Command-line argument example:
◦ Run: uv run agent.py dev '{"user_name":"Alice"}'
◦ In `agent.py`:
import sys, json
simulated_metadata = json.loads(sys.argv[2]) if len(sys.argv) > 2 else {}
# inject into your startup logic / pass to session handler
• Environment variable example:
◦ Run: METADATA_USER_NAME=Alice uv run agent.py dev
◦ In code:
import os
user_name = os.getenv("METADATA_USER_NAME")
Which would you prefer to use for testing — simulate via env/args or run an actual dispatch so ctx.job.metadata is populated?
Sources: Job lifecycle | LiveKit docs | Agent dispatch | LiveKit docsbig-school-62939
12/16/2025, 6:05 PMtall-airline-16756
12/16/2025, 6:05 PMctx.job.metadata.
1) Add metadata when creating the dispatch (CLI)
• Example:
lk dispatch create \
--agent-name my-agent \
--room test-room \
--metadata '{"user_name":"Alice","user_id":"12345"}'
2) Or include metadata in the dispatch API request (if using the API): send the same JSON in the dispatch payload's metadata field.
3) Read metadata in your agent startup code
• In the agent entrypoint/session handler, parse `ctx.job.metadata`:
import json
@server.rtc_session()
async def my_agent(ctx: JobContext):
metadata = json.loads(ctx.job.metadata) if ctx.job.metadata else {}
user_name = metadata.get("user_name")
# continue startup using metadata
Notes
• ctx.job.metadata contains the JSON string you passed during dispatch; parse it to access fields.
• If you want help forming a specific lk dispatch create command or the API payload you plan to use, share the agent name and fields you want in metadata and I’ll give the exact command/payload.