Free How much of your firm runs on you? Take the 2-minute Key Man Score.
Lesson reading
0%

Lesson 4 / Unit 1: LiveKit Foundations

First Voice Agent

Run a Python agent in the terminal, then connect through a local LiveKit room. Check dispatch, audio tracks and interruptions before deploying.

You have API keys for a speech-to-text provider, an LLM and a text-to-speech voice. You want to hear an agent answer you, today, on your own machine.

Start in console mode to check the voice pipeline. Then use a local room to exercise the network and dispatch path.

Local Requirements

You need Python, the local server and provider credentials. Provider calls may be billed:

  • Python 3.10 or newer and the uv package manager, which LiveKit’s own quickstart uses.
  • The LiveKit server and CLI on your machine. Step 1 installs both.
  • API keys for three providers: one each for STT, LLM and TTS. The example below uses the three providers from LiveKit’s own plugin example so every line can be checked against the docs. Swap any of them for the provider you prefer; each is one plugin and one environment variable.

The local server here is self-hosted, so the agent calls providers through plugins with your own keys (LiveKit Cloud’s built-in model access is not available against a local server).

Agent Setup

1. Install the server and the CLI

On macOS:

brew install livekit livekit-cli

For Linux, use the install scripts:

curl -sSL https://get.livekit.io | bash # livekit-server
curl -sSL https://get.livekit.io/cli | bash # lk, the LiveKit CLI

2. Start the server in dev mode

livekit-server --dev

Dev mode starts a single node with a fixed key pair, devkey and secret, and binds the signalling port to 127.0.0.1:7880. Leave this terminal running. The local setup guide has the Windows download.

3. Create the project and install the agents framework

In a second terminal:

uv init voice-agent
cd voice-agent
uv add "livekit-agents[assemblyai,cartesia,openai]~=1.8" python-dotenv

Each name in the brackets is one provider plugin, so changing provider means changing that list and the matching import.

4. Put the credentials in .env.local

LIVEKIT_URL=ws://localhost:7880
LIVEKIT_API_KEY=devkey
LIVEKIT_API_SECRET=secret
ASSEMBLYAI_API_KEY=your-key
OPENAI_API_KEY=your-key
CARTESIA_API_KEY=your-key

The first three point the agent at your dev server, and when you move to LiveKit Cloud or your own EC2 fleet, these credentials change. Cloud-only integrations and production controls need a separate review.

5. Write agent.py

from dotenv import load_dotenv
from livekit import agents
from livekit.agents import Agent, AgentServer, AgentSession
from livekit.plugins import assemblyai, cartesia, openai
load_dotenv(".env.local")
server = AgentServer()
@server.rtc_session()
async def entrypoint(ctx: agents.JobContext):
session = AgentSession(
stt=assemblyai.STT(language="en"),
llm=openai.responses.LLM(model="gpt-5.3-chat-latest"),
tts=cartesia.TTS(model="sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"),
)
await session.start(
room=ctx.room,
agent=Agent(instructions="You are a concise, helpful voice assistant."),
)
if __name__ == "__main__":
agents.cli.run_app(server)

The provider choices are examples. Verify model and voice access in your accounts; keep uv.lock so the environment can be reproduced. This lesson has not been end-to-end tested against paid providers during the editorial review.

6. Talk to it in your terminal

uv run agent.py console

Console mode runs one session through your microphone and speakers without touching the LiveKit server at all, which makes it the fastest loop you will get: edit, restart, speak. Press Ctrl+T to switch to typing if you are somewhere you cannot talk.

7. Talk to it through a real room

Now run it the way production does:

uv run agent.py dev

The agent registers with your dev server and waits, reloading whenever you save the file.

Create a token for yourself, then join a room from the browser:

lk token create --api-key devkey --api-secret secret \
--join --room my-first-room --identity user1 --valid-for 24h

Open example.livekit.io, enter ws://localhost:7880 and the token, and connect. The room is created when you join, the server dispatches a job, and the agent joins a moment later. Say hello.

Pipeline Components

Every line in that file is a production concept in miniature. Here is the mapping.

  • AgentServer() is the worker. On start it registers with the LiveKit server and reports that it can take jobs. Current releases call it an agent server; we keep calling it a worker, which is what the lifecycle and load lessons count.
  • @server.rtc_session() marks your entrypoint. Because it has no agent_name, the server uses automatic dispatch and sends this agent into every new room. That is ideal on a laptop and something you will change for production, where LiveKit’s dispatch guide recommends explicit dispatch.
  • entrypoint(ctx) runs once per job, in a fresh process holding the room. One call, one process. That process is what the memory lesson measures and what the shutdown leak keeps alive by accident.
  • AgentSession(...) wires the audio pipeline: it subscribes to the caller’s track, runs STT, LLM and TTS, and publishes the agent’s voice back into the room as its own track.
  • agents.cli.run_app(server) is what gives you console, dev and start from one file. The startup modes reference lists every flag.

The round trip you hear, from the end of your sentence to the first syllable of the reply, is your latency budget. On a laptop it is whatever it is. In production you defend it.

Conversation Checks

Once it answers, stop being polite. Do these four things, in order:

  1. Talk to it normally for a few turns.
  2. Interrupt it mid-sentence.
  3. Go silent for ten seconds.
  4. Talk over it continuously.

Five minutes of adversarial poking teaches you more about turn detection and interruption handling than an afternoon of reading. It also calibrates the production behaviour you are aiming for, so you notice when a later change makes it worse.

Production Changes

The entrypoint remains useful, but a production agent also needs authentication, explicit dispatch, shutdown handling, telemetry and failure recovery. Unit 2 builds the surrounding stack. Your dev server is one process on loopback. Production is an SFU with a public IP, a UDP port range sized as a capacity decision, TURN, Redis and a SIP service.

Next: deploy the LiveKit server on AWS EC2, starting with the one port rule that decides whether media flows at all.

Common questions

Can you run a LiveKit voice agent locally?

Yes. Run livekit-server --dev in one terminal and your agent on the agents framework in another. The framework is the same, but production needs additional lifecycle, security and observability work.

What is the difference between console mode and dev mode in LiveKit Agents?

Console mode runs a single session in your terminal using your microphone and speakers, without connecting to a LiveKit server. Dev mode registers the agent with a real LiveKit server, reloads on code changes, and gets dispatched into rooms like a production worker.

We do this work for teams shipping voice agents: Self-Hosted Voice Agent .

Edit this lesson on GitHub ↗

Discussion

Questions, corrections and war stories welcome. Sign in with GitHub to comment.

Free guide

Be The 10x Entrepreneur

Get the firm out of your head.

  • 8 chapters
  • 7 worksheets
  • 30 day plan