how can i deploy two versions of my agent from the...
# ask-ai
a
how can i deploy two versions of my agent from the same repo? say i want for production that get dispatched to room with real users and i want another agent that is for dev/staging purposes on test devices)
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to deploy two versions of the same agent from one repo—one for production (real users) and one for dev/staging (test devices). You can do this cleanly by keeping separate environment configs and secrets and deploying each agent separately. Recommended approach (concise): 1. Create separate config files • Example:
livekit.production.toml
and
livekit.staging.toml
. • Give each file a distinct agent ID and project/subdomain. 1. Deploy using the CLI with the config flag • Staging: a.
lk agent create --config livekit.staging.toml
b.
lk agent deploy --config livekit.staging.toml
• Production: a.
lk agent create --config livekit.production.toml
b.
lk agent deploy --config livekit.production.toml
1. Isolate environments • Use separate LiveKit projects (or at least separate agent IDs and credentials) to keep staging traffic off production rooms. • Use distinct room naming conventions for staging vs prod. 1. Manage secrets per environment • Supply environment-specific secrets with
--secrets-file
or environment variables so keys/credentials don’t cross environments. Optional choices • If both agents must run in the same project, use explicit dispatch or unique agent names so you control which agent handles which rooms/users. • For quick prototyping, use Agent Builder for staging then download and deploy the same repo for production. If you want, tell me: • Do you already have separate LiveKit projects or do you plan to run both agents in the same project? • I can then give exact example
livekit.toml
contents and CLI commands tailored to your setup. Sources: Deploying to custom environments | LiveKit docs | Agent deployment CLI reference | LiveKit docs | Deploying to production | LiveKit Docs | Agent Builder | LiveKit docs
a
how to properly manage different names for each agent? can you show me examples please for this setup
t
You're asking how to name/manage multiple agents (prod + staging) from one repo and want examples. Below are concise naming best practices and concrete
livekit.toml
examples plus CLI commands and an explicit-dispatch example. Best practices • Use clear environment suffixes:
-prod
,
-staging
,
-dev
. • Include app or service name for context:
voicebot-prod
,
voicebot-staging
. • Keep agent IDs stable (don’t regenerate) so routing and logs remain consistent. • Keep separate secrets and projects when possible; otherwise use unique agent IDs and room naming conventions. • Add metadata in the config or repo (e.g., README) documenting each agent’s purpose. Example 1 — Separate LiveKit projects (recommended) livekit.production.toml
Copy code
[project]
subdomain = "myapp-production"

[agent]
id = "voicebot-prod"
name = "VoiceBot (production)"
livekit.staging.toml
Copy code
[project]
subdomain = "myapp-staging"

[agent]
id = "voicebot-staging"
name = "VoiceBot (staging)"
Deploy commands: 1. Create/deploy staging •
lk agent create --config livekit.staging.toml --secrets-file staging.secrets
•
lk agent deploy --config livekit.staging.toml --secrets-file staging.secrets
1. Create/deploy production •
lk agent create --config livekit.production.toml --secrets-file production.secrets
•
lk agent deploy --config livekit.production.toml --secrets-file production.secrets
Example 2 — Same project, distinct agents (use explicit dispatch) livekit.common-project.toml
Copy code
[project]
subdomain = "myapp"  # single LiveKit project

[agent]
id = "voicebot-prod"
name = "VoiceBot (production)"
Alternate config for staging:
Copy code
[project]
subdomain = "myapp"

[agent]
id = "voicebot-staging"
name = "VoiceBot (staging)"
Routing strategies when in same project: • Use explicit dispatch to route test devices to
voicebot-staging
and real users to
voicebot-prod
. • Use distinct room name prefixes:
staging-<room>
vs
prod-<room>
. Explicit-dispatch example (concept) • Client or backend requests an agent by name when creating/entering a room: ◦ Test devices request
voicebot-staging
◦ Real users request
voicebot-prod
If you want, I can: • Provide ready-to-use
livekit.toml
files matching your repo structure, or • Show an explicit-dispatch request example tailored to your client SDK (which SDK are you using?).
a
also i use explicitt dispatch, so i pass the agent_name in the worker, how can i achieve that too?
t
You're asking how to pass
agent_name
in the worker for explicit dispatch and manage distinct names for prod vs staging. Below are concise examples and the exact pieces you need to change in your code, configs, and dispatch token. 1) Choose agent names (best practice) • production:
voicebot-prod
• staging:
voicebot-staging
2) Worker examples — set the agent name so worker only responds to explicit dispatch • Python (AgentServer decorator)
Copy code
from livekit.agents import AgentServer, JobContext

server = AgentServer()

@server.rtc_session(agent_name="voicebot-prod")
async def agent_prod(ctx: JobContext):
    # production logic
    pass

# for staging, run the same repo but with agent_name="voicebot-staging"
• Node.js (WorkerOptions)
Copy code
import { WorkerOptions, Worker } from '@livekit/agents';

const prodOpts: WorkerOptions = {
  agentName: 'voicebot-prod',
  // other options (project, key/secret via env or secrets file)
};

const worker = new Worker(prodOpts);
3) Config files — separate
livekit.toml
per environment • livekit.prod.toml
Copy code
[project]
subdomain = "myapp-production"

[agent]
id = "CA_voicebot_prod"
name = "voicebot-prod"
• livekit.staging.toml
Copy code
[project]
subdomain = "myapp-staging"

[agent]
id = "CA_voicebot_staging"
name = "voicebot-staging"
4) Deploy commands (example) • Create/deploy staging: a.
lk agent create --config livekit.staging.toml --secrets-file staging.secrets
b.
lk agent deploy --config livekit.staging.toml --secrets-file staging.secrets
• Create/deploy production: a.
lk agent create --config livekit.prod.toml --secrets-file prod.secrets
b.
lk agent deploy --config livekit.prod.toml --secrets-file prod.secrets
5) Dispatch users to a specific agent — include agent name in room configuration on the access token • Python (token with RoomConfiguration)
Copy code
from livekit.api import AccessToken, RoomAgentDispatch, RoomConfiguration, VideoGrants

token = (
    AccessToken()
    .with_identity("user-123")
    .with_grants(VideoGrants(room_join=True, room="my-room"))
    .with_room_config(
        RoomConfiguration(agents=[
            RoomAgentDispatch(agent_name="voicebot-prod", metadata='{"user_id":"123"}')
        ])
    )
    .to_jwt()
)
• Node.js (server SDK)
Copy code
import { AccessToken } from 'livekit-server-sdk';
import { RoomConfiguration, RoomAgentDispatch } from '@livekit/protocol';

const at = new AccessToken(apiKey, apiSecret);
at.identity = 'user-123';
at.addGrant({ roomJoin: true, room: 'my-room' });
at.roomConfig = new RoomConfiguration({
  agents: [new RoomAgentDispatch({ agentName: 'voicebot-prod', metadata: '{"user_id":"123"}' })],
});
const token = await at.toJwt();
6) Notes & checklist • Ensure the worker's
agent_name
exactly matches the
agent_name
used in the dispatch token. • Run separate workers (processes/containers) for prod and staging using different env/config files and secrets. • Use distinct LiveKit projects or at minimum distinct agent IDs and room name prefixes (
staging-...
vs
prod-...
) to avoid accidental routing. • Keep
agent_name
stable after deployment to avoid breaking dispatch rules. Want me to generate complete
livekit.toml
files and exact CLI commands for your repo structure, or show a sample CI step that deploys both agents? Which SDK/language is your worker using (Python or Node)? Sources: Agent dispatch | LiveKit docs | Agent Builder | LiveKit docs
a
but the name in livekit.toml is picked up or we need to deploy it separate?
t
You're asking whether
agent_name
is picked up from
livekit.toml
or needs separate deployment — it does not get picked up from
livekit.toml
. The
agent_name
is set in your agent code (or via an env var) and must match the name used in explicit dispatch. Short checklist and examples: Key facts •
livekit.toml
holds project/agent infrastructure metadata (project subdomain, agent id), not the runtime
agent_name
used for dispatch. • Your worker must run with the correct
agent_name
(hardcoded or from an environment variable). • Dispatch tokens or API calls must reference the exact
agent_name
the worker uses. Quick examples 1) Make
agent_name
environment-driven (recommended) • Python
Copy code
import os
agent_name = os.getenv("AGENT_NAME", "voicebot-default")

@server.rtc_session(agent_name=agent_name)
async def agent(ctx):
    ...
• Node.js
Copy code
const opts = { agentName: process.env.AGENT_NAME || 'voicebot-default', /*...*/ };
const worker = new Worker(opts);
2) Deploy/process strategy • Use two configs and secrets: ◦
livekit.prod.toml
+ container/process with
AGENT_NAME=voicebot-prod
◦
livekit.staging.toml
+ container/process with
AGENT_NAME=voicebot-staging
• Deploy both from the same repo; run separate processes/containers for each env. 3) Dispatch must match • When creating tokens or room config, include the matching agent name: ◦ e.g. RoomAgentDispatch(agent_name="voicebot-prod") → routes to workers running with
AGENT_NAME=voicebot-prod
. If you want, I can: • Generate exact
livekit.toml
+ Docker/CI examples using your repo layout, or • Show a token-generation snippet for the SDK/language you use. Which would you prefer? Sources: Deploying to production | LiveKit Docs | Agent Builder | LiveKit docs | Agent deployment CLI reference | LiveKit docs | Agents telephony integration | LiveKit docs | Agent dispatch | LiveKit docs
a
i had this previous version of agents code:
Copy code
if __name__ == "__main__":
    # Run the agent application
    cli.run_app(
        WorkerOptions(
            entrypoint_fnc=entrypoint,
            agent_name=os.getenv("AGENT_NAME", AGENT_NAME),
            shutdown_process_timeout=30,
        )
    )
how should i migrate it?
t
You're already almost there — migrate by making the agent name environment-driven and ensuring your deployment sets AGENT_NAME per environment. Minimal changes and examples below. 1) Keep env-driven pattern (recommended)
Copy code
import os
from livekit.agents import WorkerOptions, cli

if __name__ == "__main__":
    cli.run_app(
        WorkerOptions(
            entrypoint_fnc=entrypoint,
            agent_name=os.getenv("AGENT_NAME", "voicebot-default"),
            shutdown_process_timeout=30,
        )
    )
2) Alternative: read from a config file
Copy code
import os, toml
cfg = toml.load("livekit.env.toml")
agent_name = os.getenv("AGENT_NAME") or cfg.get("agent", {}).get("name", "voicebot-default")

cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, agent_name=agent_name, ...))
3) Deployment pattern (what to run for each env) • Staging container/process: set env
AGENT_NAME=voicebot-staging
and use
livekit.staging.toml
+
staging.secrets
. • Production container/process: set env
AGENT_NAME=voicebot-prod
and use
livekit.prod.toml
+
prod.secrets
. 4) Dispatch tokens must match • Ensure RoomAgentDispatch or room token uses the exact same agent name you set via
AGENT_NAME
. If you'd like, I can: • Generate a Dockerfile/compose or CI job snippet showing how to deploy both agents from the same repo, or • Produce exact
livekit.*.toml
+ secrets examples for your setup. Which do you want?
a
but how to use AgentServer class?
t
You're migrating from using
WorkerOptions
+
cli.run_app(...)
to the
AgentServer
pattern and want to know how to set
agent_name
there. Keep the environment-driven approach and register your entrypoint on an AgentServer with the same agent name used for dispatch. Minimal migration example (Python): 1) AgentServer + env-driven agent name
Copy code
import os
from livekit.agents import AgentServer, JobContext, agents, cli

agent_name = os.getenv("AGENT_NAME", "voicebot-default")

server = AgentServer()

@server.rtc_session(agent_name=agent_name)
async def entrypoint(ctx: JobContext):
    # your per-job logic here
    ...
2) Optional: prewarm resources
Copy code
def prewarm(proc):
    proc.userdata["vad"] = load_vad_model()

server.setup_fnc = prewarm
3) Start server (same repo, different processes/containers per env)
Copy code
if __name__ == "__main__":
    cli.run_app(server)
Deployment notes • Run two processes/containers from the same repo with different env vars: ◦ Staging:
AGENT_NAME=voicebot-staging
+
livekit.staging.toml
+ staging secrets ◦ Prod:
AGENT_NAME=voicebot-prod
+
livekit.prod.toml
+ prod secrets • Ensure RoomAgentDispatch tokens or dispatch rules use the exact same
agent_name
. •
livekit.toml
does not set `agent_name`; the name must be set in code or via env var. If you want, I can convert your existing entrypoint function into a complete AgentServer example using your real entrypoint code and produce Docker/CI snippets for running both agents. Which would you like? Sources: Agent server options | LiveKit docs | Agents telephony integration | LiveKit docs | Agents v0.x migration guide - Python | LiveKit docs
a
and to point to different .env vars, how do i do it?
t
You can load different .env files per environment and pass those values into your AgentServer worker. Two common approaches: 1) load the correct .env at process start, or 2) use your container/CI to inject env vars. Examples below are minimal and work with the AgentServer pattern you migrated to. 1) Load .env at process start (python-dotenv) • Install:
pip install python-dotenv
• Project layout: ◦ .env.prod ◦ .env.staging ◦ run_prod.py ◦ run_staging.py • run_prod.py
Copy code
from dotenv import load_dotenv
import os
from my_agent import server  # AgentServer instance

load_dotenv(".env.prod")
# now env vars (AGENT_NAME, API_KEY, etc.) are available
if __name__ == "__main__":
    cli.run_app(server)
• run_staging.py
Copy code
from dotenv import load_dotenv
import os
from my_agent import server

load_dotenv(".env.staging")
if __name__ == "__main__":
    cli.run_app(server)
• In your agent code use env vars:
Copy code
agent_name = os.getenv("AGENT_NAME", "voicebot-default")

@server.rtc_session(agent_name=agent_name)
async def entrypoint(ctx: JobContext):
    ...
2) Single entrypoint, select env file via an env var
Copy code
env_file = os.getenv("ENV_FILE", ".env")
load_dotenv(env_file)
Start with:
ENV_FILE=.env.prod python -m my_agent_run
3) Docker examples • Dockerfile (build once)
Copy code
FROM python:3.11
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "run.py"]
• docker-compose.yml (two services)
Copy code
version: "3.8"
services:
  agent-prod:
    build: .
    env_file: .env.prod
    command: ["python", "run_prod.py"]
  agent-staging:
    build: .
    env_file: .env.staging
    command: ["python", "run_staging.py"]
4) CI/CD / Kubernetes • CI job: set env vars per job or pass the appropriate .env file into the container. • Kubernetes: use separate Deployments with distinct ConfigMaps/Secrets and set
AGENT_NAME
and other secrets per Deployment. 5) Checklist • Ensure
AGENT_NAME
in the env file matches the value used in tokens/dispatch rules. • Keep secrets in secure secrets storage (Kubernetes Secrets, cloud secret manager) instead of plaintext .env in production. • Use different
livekit.toml
/ secrets files per environment when deploying via
lk agent deploy
. If you want, I can generate: • ready-to-use `run_prod.py`/`run_staging.py` and Docker Compose for your repo, or • a Kubernetes Deployment + Secret manifest for prod and staging. Which would help most?
a
yes please, i want the full setup for deploying the prod or dev agent, picking the corresponding .env and livekit.toml
t
Below is a complete, ready-to-adapt setup to run two agents (prod and staging/dev) from the same repo. It includes: AgentServer code, per-environment .env files,
livekit.toml
files, small runner scripts, Dockerfile, docker-compose for local runs, and example deploy/CLI commands. Secrets advice and checklist are included at the end. 1) Repo layout (example) • my_agent/ ◦ app/ ▪︎ agent_server.py ▪︎ entrypoint.py ◦ run_prod.py ◦ run_staging.py ◦ .env.prod ◦ .env.staging ◦ livekit.prod.toml ◦ livekit.staging.toml ◦ Dockerfile ◦ docker-compose.yml ◦ requirements.txt 2) AgentServer code (app/agent_server.py)
Copy code
import os
from livekit.agents import AgentServer, JobContext, cli

agent_name = os.getenv("AGENT_NAME", "voicebot-default")

server = AgentServer()

@server.rtc_session(agent_name=agent_name)
async def entrypoint(ctx: JobContext):
    # Import or implement your actual agent logic here
    from .entrypoint import run_agent_session
    await run_agent_session(ctx)
3) Example entrypoint logic stub (app/entrypoint.py)
Copy code
async def run_agent_session(ctx):
    # Example: access env vars, secrets, and ctx data
    api_key = os.getenv("MY_API_KEY")
    # Implement actual agent behavior here
    # e.g., create AgentSession, start streams, handle events
    print("Starting agent for room:", ctx.room.name if hasattr(ctx, "room") else "unknown")
    # placeholder for real logic
    await ctx.wait_for_end()
4) Runner scripts that load per-environment .env files • run_prod.py
Copy code
from dotenv import load_dotenv
import os
from app import agent_server as server_module
from livekit.agents import cli

load_dotenv(".env.prod")  # loads AGENT_NAME, API keys, etc.

if __name__ == "__main__":
    cli.run_app(server_module.server)
• run_staging.py
Copy code
from dotenv import load_dotenv
import os
from app import agent_server as server_module
from livekit.agents import cli

load_dotenv(".env.staging")

if __name__ == "__main__":
    cli.run_app(server_module.server)
5) Example .env files (do NOT commit secrets to git) • .env.prod
Copy code
AGENT_NAME=voicebot-prod
MY_API_KEY=prod_api_key_here
LIVEKIT_API_KEY=prod_livekit_key
LIVEKIT_API_SECRET=prod_livekit_secret
# other prod secrets...
• .env.staging
Copy code
AGENT_NAME=voicebot-staging
MY_API_KEY=staging_api_key_here
LIVEKIT_API_KEY=staging_livekit_key
LIVEKIT_API_SECRET=staging_livekit_secret
# other staging secrets...
6) livekit.toml files • livekit.prod.toml
Copy code
[project]
subdomain = "myapp-production"

[agent]
id = "CA_voicebot_prod"
name = "voicebot-prod"
• livekit.staging.toml
Copy code
[project]
subdomain = "myapp-staging"

[agent]
id = "CA_voicebot_staging"
name = "voicebot-staging"
Note:
livekit.toml
holds infra config (project, agent id). The runtime
agent_name
is taken from
AGENT_NAME
env var in the code and must match dispatch. 7) Dockerfile (build once)
Copy code
FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . /app

# default command will be overridden by docker-compose or CI
CMD ["python", "run_prod.py"]
8) docker-compose.yml (two services for local/dev)
Copy code
version: "3.8"
services:
  agent-prod:
    build: .
    env_file:
      - .env.prod
    volumes:
      - .:/app
    command: ["python", "run_prod.py"]
  agent-staging:
    build: .
    env_file:
      - .env.staging
    volumes:
      - .:/app
    command: ["python", "run_staging.py"]
9) requirements.txt (example)
Copy code
python-dotenv
livekit-agents
# other deps your agent needs
10) Deploy steps / CLI commands • Create or update agent infra (if needed) ◦ Staging: ▪︎ lk agent create --config livekit.staging.toml --secrets-file staging.secrets ▪︎ lk agent deploy --config livekit.staging.toml --secrets-file staging.secrets ◦ Production: ▪︎ lk agent create --config livekit.prod.toml --secrets-file prod.secrets ▪︎ lk agent deploy --config livekit.prod.toml --secrets-file prod.secrets • Run locally with docker-compose: ◦ docker-compose up --build agent-staging ◦ docker-compose up --build agent-prod ◦ Or run both: docker-compose up --build • Run directly (no Docker): ◦ python run_staging.py ◦ python run_prod.py 11) Token / dispatch reminder • Your RoomAgentDispatch must use the exact
agent_name
(AGENT_NAME) value. Example when generating tokens: ◦ agent_name = "voicebot-prod" for prod ◦ agent_name = "voicebot-staging" for staging 12) Secrets & best practices checklist • Never commit
.env.*
with secrets. Use secret stores (Kubernetes Secrets, cloud secret manager) in production. • Use distinct LiveKit projects or at least distinct agent IDs and credentials for staging vs prod to avoid cross-traffic. • Keep
AGENT_NAME
stable once deployed; changing it requires updating dispatch rules and tokens. • For Kubernetes, create two Deployments (prod/staging) each with their own Secret/ConfigMap and container command: ◦ container.command = ["python", "run_prod.py"] with envFrom: prod secret ◦ container.command = ["python", "run_staging.py"] with envFrom: staging secret 13) Optional: single-run entry allowing ENV_FILE selection If you prefer a single runner, allow selecting .env file at start:
Copy code
# run.py
from dotenv import load_dotenv
import os
from app import agent_server as server_module
from livekit.agents import cli

env_file = os.getenv("ENV_FILE", ".env.prod")
load_dotenv(env_file)

if __name__ == "__main__":
    cli.run_app(server_module.server)
Start with:
ENV_FILE=.env.staging python run.py
14) Next steps I can provide • A ready-to-use Docker Compose or Kubernetes manifest tailored to your repo structure. • A CI job example (GitHub Actions) that builds an image and deploys both prod and staging with correct secrets. Which one would you like me to generate next: Docker Compose full example ready to run, or Kubernetes manifests (Deployment + Secret) for prod and staging?
a
will this work for running agent locally (with uv run python src/agent_server.py start/dev) ? do i need to replace the Dockerfile?
t
Yes — the AgentServer setup will work locally with a command like `uvicorn`/`python` as long as you load the correct .env before starting the server. You do not need to replace the Dockerfile just to run locally; the Dockerfile is only for container builds. How to run locally • Ensure your server file loads the desired env file at process start (example at top of
src/agent_server.py
or in a small runner):
Copy code
from dotenv import load_dotenv
import os
from app import agent_server  # AgentServer instance
from livekit.agents import cli

# choose env file, set ENV_FILE or default
load_dotenv(os.getenv("ENV_FILE", ".env.dev"))

if __name__ == "__main__":
    cli.run_app(agent_server.server)
• Start locally: ◦ With python:
ENV_FILE=.env.prod python src/agent_server.py
◦ Or for autoreload during development with uvicorn or similar:
ENV_FILE=.env.dev uvicorn src.agent_server:app --reload
(use your dev runner or the
run
entry you created) Notes •
AGENT_NAME
must be in the chosen .env so the
@server.rtc_session(agent_name=...)
picks it up. • If you prefer separate entry scripts, keep
run_prod.py
/
run_staging.py
that call
load_dotenv(...)
then
cli.run_app(...)
. • Dockerfile: keep it as-is for containerized deploys. No change required for local runs. You only need to build/change it if you want a different container image or commands. If you share how you currently start the app locally (exact command or runner), I’ll give the precise command you should use for prod vs dev.
a
and what if i want to use one of the deployed agent i have now but change their naming
t
You can rename a deployed agent, but do it carefully to avoid breaking dispatch. Below are safe, minimal steps and options — tell me which matches your setup (single project or separate projects, and whether you can run both names during a transition). 1) Quick summary of approach • Change the runtime
agent_name
(env var or code). • Deploy the worker with the new name as a separate process (do not replace the running one immediately). • Update dispatch rules/tokens to point to the new name. • Once traffic is validated, remove the old worker or stop accepting dispatch to the old name. 2) Safe step-by-step (recommended rollout) 1. Add new name to code/env ◦ Set
AGENT_NAME=new-name
(or update code to read
AGENT_NAME
). 1. Start a new worker/process with the new name ◦ Use a separate process/container so old agent keeps running: ▪︎ e.g.,
ENV_FILE=.env.prod_new AGENT_NAME=voicebot-v2 python run_prod.py
1. Update dispatch ◦ Change RoomAgentDispatch in token generation or alter dispatch rules to reference
agent_name=new-name
. ◦ Route a small percentage of test or canary users to new-name first. 1. Validate ◦ Monitor logs, sessions, and behavior for the new agent. 1. Decommission old agent ◦ Stop the old process/container or update deploy to remove it after confirming stability. 1. Cleanup ◦ Update configs, README, and any infra that referenced the old name. 3) If you cannot run both simultaneously • You must schedule a brief cutover: ◦ Stop old worker. ◦ Deploy new worker with
AGENT_NAME=new-name
. ◦ Immediately update all dispatch tokens/rules to new-name. ◦ Risk: any in-flight tokens that still reference old-name will not be dispatched to the new worker. 4) Notes & gotchas •
livekit.toml
does not control `agent_name`; the name must match the worker runtime env/code and the dispatch token/rule. • Keep agent IDs (the infra
agent.id
in
livekit.toml
) stable if you rely on them for infra metadata; agent_name is orthogonal. • If you change
agent_name
, update monitoring/alerting, logs, and permissions references that filter by name. • Use short-lived tokens or a roll-forward strategy to avoid long-lived tokens pointing to the old name after cutover. Tell me: • Are you running both agents in the same LiveKit project or separate projects? • Do you want example CLI/token changes for your language (Python or Node)?
a
tbh, i want to delete current agent and deploy the two we have from scratch with correct naming, still we don't have active traffic so we can do it safely
t
Great — since you have no active traffic and want to delete the current agent and redeploy two agents from scratch, here are the exact, minimal steps to do that safely, with commands and files to run. 1) Delete the existing agent • Find current agent ID or list agents: ◦
lk agent status
• Delete it: ◦
lk agent delete --id YOUR_CURRENT_AGENT_ID
2) Prepare repo files (ensure .env files excluded from git) • .env.prod (secrets, AGENT_NAME=voicebot-prod) • .env.dev (secrets, AGENT_NAME=voicebot-staging) • livekit.prod.toml (project subdomain + agent.id) • livekit.dev.toml (project subdomain + agent.id) • AgentServer code reads AGENT_NAME from env (example below) 3) AgentServer (Python) — single codebase, env-driven agent name app/agent_server.py
Copy code
import os
from livekit.agents import AgentServer, JobContext, cli

agent_name = os.getenv("AGENT_NAME", "voicebot-default")
server = AgentServer()

@server.rtc_session(agent_name=agent_name)
async def entrypoint(ctx: JobContext):
    # call your agent logic here
    from .entrypoint import run_agent_session
    await run_agent_session(ctx)
4) Runners that load correct .env run_prod.py
Copy code
from dotenv import load_dotenv
from app import agent_server
from livekit.agents import cli

load_dotenv(".env.prod")
if __name__ == "__main__":
    cli.run_app(agent_server.server)
run_dev.py
Copy code
from dotenv import load_dotenv
from app import agent_server
from livekit.agents import cli

load_dotenv(".env.dev")
if __name__ == "__main__":
    cli.run_app(agent_server.server)
5) Create livekit.toml configs (example) livekit.prod.toml
Copy code
[project]
subdomain = "myapp-production"

[agent]
id = "CA_voicebot_prod"
name = "voicebot-prod"
livekit.dev.toml
Copy code
[project]
subdomain = "myapp-staging"

[agent]
id = "CA_voicebot_staging"
name = "voicebot-staging"
6) Deploy each agent with the CLI • Create/deploy dev: ◦
lk agent create --region us-east --config livekit.dev.toml --secrets-file .env.dev .
◦ Verify:
lk agent status --config livekit.dev.toml
• Create/deploy prod: ◦
lk agent create --region us-east --config livekit.prod.toml --secrets-file .env.prod .
◦ Verify:
lk agent status --config livekit.prod.toml
7) Run locally (optional) — no Docker change required • Run dev locally:
ENV_FILE=.env.dev python run_dev.py
(or
python run_dev.py
) • Run prod locally:
ENV_FILE=.env.prod python run_prod.py
• Ensure
AGENT_NAME
is present in the .env files so
@server.rtc_session(agent_name=...)
matches 8) Dispatch tokens must match agent_name • When generating RoomAgentDispatch or token claims, use the exact `AGENT_NAME`: ◦ dev:
agent_name="voicebot-staging"
◦ prod:
agent_name="voicebot-prod"
9) Post-deploy validation & cleanup • Test routing to each agent with a test token for each name. • Monitor logs: ◦
lk agent logs --config livekit.dev.toml
◦
lk agent logs --config livekit.prod.toml
• When happy, remove any old infra references. 10) Security notes • Do not commit
.env.*
to git. Use secret stores for production. • Use
--secrets-file
when creating/deploying to avoid embedding secrets in configs. If you want, I can: • Generate a Docker Compose file that runs both agents locally using
.env.prod
and
.env.dev
, or • Produce the exact
lk agent create
commands filled with your project subdomains/agent IDs. Which would you like? Sources: Deploying to production | LiveKit Docs | Agent deployment CLI reference | LiveKit docs | Secrets management | LiveKit docs
a
can i assign id by hand or it has to be the autogenerated by Livekit?
t
You can set an agent ID yourself — it does not have to be the autogenerated one. The agent ID (the
agent.id
in
livekit.toml
or the
--id
you pass to the CLI) is separate from the
agent_name
used for explicit dispatch. Key points and an example: • The agent ID is infrastructure metadata (used by the LiveKit CLI and dashboard). It must be unique within your project. • The
agent_name
is the runtime name your worker exposes for explicit dispatch and must match whatever you put in tokens/dispatch rules. • You can provide a custom ID when configuring or creating an agent with the CLI or by editing
livekit.toml
. Example patterns: • In `livekit.prod.toml`:
Copy code
[project]
subdomain = "myapp-production"

[agent]
id = "CA_voicebot_prod"        # custom ID you choose
name = "voicebot-prod"        # infra name (optional metadata)
• CLI create with explicit ID:
Copy code
lk agent create --id CA_voicebot_prod --config livekit.prod.toml --secrets-file .env.prod .
Recommendations: • Pick a stable, descriptive ID (prefix with
CA_
or your org) so you can reference it in scripts. • Keep
agent.id
stable across redeploys if you need continuity in the dashboard/logs, but changing it is fine if you want a clean start. • Ensure
AGENT_NAME
used by your worker/runtime matches the
agent_name
used in dispatch tokens;
agent.id
and
agent_name
can be different strings. If you want, tell me the ID/name you'd like to use and I’ll generate the exact
livekit.toml
and
lk
commands for prod and staging.
a
and every time i want to deploy a new version to either agent, what command should i run?
t
Use
lk agent deploy
with the environment's config and secrets. After initial
lk agent create
, subsequent updates use
lk agent deploy
. Minimal examples: • Deploy new version to staging: ◦
lk agent deploy --config livekit.staging.toml --secrets-file .env.staging .
• Deploy new version to production: ◦
lk agent deploy --config livekit.prod.toml --secrets-file .env.prod .
Notes: • The trailing
.
is the path to your code bundle (current dir). Adjust if your build artifact lives elsewhere. • Ensure
AGENT_NAME
and other env vars in the referenced
.env
match the agent runtime and dispatch rules. • To check status or logs after deploy: ◦
lk agent status --config livekit.staging.toml
◦
lk agent logs --config livekit.staging.toml
◦ (Replace
.staging.
with
.prod.
for production) If you use CI, run the same commands in the job that builds your artifact (or point
--config
and
--secrets-file
to CI-managed files/secrets). Would you like a sample CI job for this?
a
the agent name wasn't picked up on deployment to livekit cloud, tho when running locally they were picked up
t
You're right to notice the difference. agent_name is a runtime value your worker must set (in code or env); it is not read from livekit.toml or your local .env automatically when deployed. To fix this in LiveKit Cloud, ensure the AGENT_NAME env var is injected into the deployed container. Quick checklist and commands to make deployed agents pick up agent_name: 1) Hardcode or read from env in code (example)
Copy code
agent_name = os.getenv("AGENT_NAME", "voicebot-default")

@server.rtc_session(agent_name=agent_name)
async def entrypoint(ctx: JobContext):
    ...
2) Provide AGENT_NAME to the deployed agent via secrets/envs • Using a secrets file: lk agent deploy --config livekit.prod.toml --secrets-file .env.prod . (Ensure .env.prod contains AGENT_NAME=voicebot-prod) • Or pass a secret inline: lk agent deploy --config livekit.prod.toml --secrets AGENT_NAME=voicebot-prod . 3) Verify secret was applied • List agent status / secrets: lk agent status --config livekit.prod.toml lk agent secrets --id <agent-id> 4) Check agent logs for startup confirmation • lk agent logs --config livekit.prod.toml Look for your code printing the agent_name or the server log that shows the agent registered with that name. 5) If you ran deploy without secrets previously • Redeploy with the secrets/env injection (step 2). Local .env files are not automatically included in the cloud image. If you want, tell me: • which env file you're using (.env.prod or .env.staging) and • the exact deploy command you ran, and I’ll produce the exact
lk agent deploy
command you should run and a short log-check snippet. Sources: Deploying to production | LiveKit Docs | Agents telephony integration | LiveKit docs | Agent deployment CLI reference | LiveKit docs | Secrets management | LiveKit docs
a
when building locally, docker doesnt get the env file?
t
You're right — Docker build does not automatically include your local
.env
into the image or container. .env files are only read by the tools you run (your app, docker-compose, or explicit docker flags). Below are concise options and examples to ensure your agent sees the env vars when running locally. Key points • docker build does not inject runtime env vars; use build args only for build-time values (not secrets). • Provide env vars at container run time with
--env-file
/
-e
, or let docker-compose load an
env_file
. • For local development, load
.env
inside the container at runtime (python-dotenv) or from the host via
--env-file
. • Never bake sensitive secrets into the image. Options & examples 1) Run with docker and an env file • Build: docker build -t my-agent:local . • Run: docker run --env-file .env.dev --rm my-agent:local python run_dev.py 2) Use docker-compose (recommended for running both agents) docker-compose.yml
Copy code
version: "3.8"
services:
  agent-prod:
    build: .
    env_file: .env.prod
    command: ["python", "run_prod.py"]
  agent-staging:
    build: .
    env_file: .env.dev
    command: ["python", "run_staging.py"]
Start: docker-compose up --build 3) If you must pass a single var inline docker run -e AGENT_NAME=voicebot-prod --rm my-agent:local python run_prod.py 4) Avoid using build-args for secrets • You can pass build-time values with
--build-arg
, but those are baked into the image layers and not secure: docker build --build-arg SOME_VAR=value -t my-agent:local . • Prefer runtime env injection instead. 5) Ensure app reads env at runtime • Your app should use
os.getenv(...)
or
python-dotenv.load_dotenv()
at process start (not during build) so container-run envs are honored. Quick checklist • Use
--env-file
or docker-compose
env_file
to inject .env into the container at run time. • Confirm inside the container:
docker exec -it <container> env
or add a startup print to log
AGENT_NAME
. • Keep secrets out of images — use secret managers for production. If you want, I can: • Produce an updated Dockerfile + docker-compose pair that loads
.env
safely and prints
AGENT_NAME
on startup for verification. Would you like that?