I am testing an agent from the command line, using...
# ask-ai
b
I am testing an agent from the command line, using "uv run python". Where can I provide the values for metadata so they will be available on startup?
t
Thanks for reaching out, Mat. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking where to provide metadata so it’s available at agent startup when running locally with "uv run python". Here's how to do that and how metadata is normally delivered. Short answer • For real agent metadata (the one accessible as
ctx.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:
Copy code
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`:
Copy code
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:
Copy 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 docs
b
An actual dispatch.
t
You're running a real dispatch — here's how to supply metadata so it is available at agent startup as
ctx.job.metadata
. 1) Add metadata when creating the dispatch (CLI) • Example:
Copy code
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`:
Copy code
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.