> ## Documentation Index
> Fetch the complete documentation index at: https://docs.simli.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LiveKit Agents

> Give a LiveKit Agents voice agent a lip-synced Simli avatar

[LiveKit Agents](https://docs.livekit.io/agents/) is LiveKit's open-source Python framework for voice AI agents. With the Simli plugin, a Simli avatar joins the agent's LiveKit room: the agent sends it its voice, and the avatar publishes lip-synced video and voice on the agent's behalf, so any LiveKit frontend shows them as the agent's.

## What you need

* **A [LiveKit Cloud](https://cloud.livekit.io) project**: the free Build plan works, with no card required. You need its URL, API key and API secret. The starter runs its models on [LiveKit Inference](https://docs.livekit.io/agents/models/): Deepgram Nova-3 for speech-to-text, Gemma 4 31B as the LLM and Cartesia Sonic 3.5 for text-to-speech. The LiveKit project's credentials pay for them, so there are no OpenAI, Deepgram or other model keys.
* A Simli API key, from your [Simli dashboard](https://app.simli.com). The free plan works.
* Python 3.10 or later (the starter uses [uv](https://docs.astral.sh/uv/)), and [Node.js](https://nodejs.org) 20.9 or later for the starter's web page

> [!WARNING]
>
> A LiveKit server running on your own machine (`localhost`, or `livekit-server --dev`) won't work. Simli's servers join your room from the internet, so `LIVEKIT_URL` must be reachable from the internet. LiveKit Inference also runs only on LiveKit Cloud, so the starter needs a LiveKit Cloud project. The Simli plugin itself works with a self-hosted server with a public address too, if your agent brings its own model plugins and keys.

On the free plans, LiveKit's Build plan includes \$2.50 of LiveKit Inference credit a month, and Simli's free plan 50 avatar minutes a month and 1 session at a time. The starter's README has the details.

## Run the starter

> [card] [create-simli-app-livekit-agents](https://github.com/simliai/create-simli-app-livekit-agents)
>
> A LiveKit Agents voice agent on LiveKit Inference, with a Simli Trinity-2 avatar and a small web page to talk to it. Its lockfile pins the versions it was tested with.

```bash theme={null}
git clone https://github.com/simliai/create-simli-app-livekit-agents
cd create-simli-app-livekit-agents
uv sync
cp .env.example .env                          # then paste in your four values
cp frontend/.env.example frontend/.env.local  # then copy in the same three LIVEKIT_ values
uv run agent.py dev
```

In a second terminal, start the web page. Its token route reads the LiveKit key and secret on the server, so they never reach the browser.

```bash theme={null}
cd frontend && npm install && npm run dev
```

Open [http://localhost:3000](http://localhost:3000), click **Start** and allow the microphone. The avatar greets you once it's ready.

The agent registers as `simli-agent` and only joins rooms whose token dispatches it by that name, as the page's token does, so other agents in your LiveKit project don't join too. To rename it, set `LIVEKIT_AGENT_NAME` in both `.env` and `frontend/.env.local`. LiveKit's [Agents Playground](https://agents-playground.livekit.io) works too: connect it to your project and set **Agent name** to `simli-agent`.

## Build it yourself

### 1. Install the packages

```bash theme={null}
pip install "livekit-agents>=1.8.11" "livekit-plugins-simli>=1.8.11" python-dotenv
```

### 2. Set your keys

Put them in a `.env` file next to your agent:

```bash .env theme={null}
LIVEKIT_URL=wss://your-project.livekit.cloud
LIVEKIT_API_KEY=...
LIVEKIT_API_SECRET=...
SIMLI_API_KEY=...
```

LiveKit Inference bills the models to the project in these `LIVEKIT_*` values. The Simli plugin reads them too: it gives the avatar a token to join the room.

### 3. Add the avatar to your agent

This is the core of the starter's agent. The [starter's `agent.py`](https://github.com/simliai/create-simli-app-livekit-agents/blob/main/agent.py) has the complete file.

```python agent.py theme={null}
import asyncio
import os

from dotenv import load_dotenv
from livekit.agents import (
    Agent,
    AgentServer,
    AgentSession,
    JobContext,
    TurnHandlingOptions,
    cli,
    inference,
)
from livekit.plugins import simli

load_dotenv()

server = AgentServer()


# Explicit dispatch: the agent only joins rooms whose token names it.
@server.rtc_session(agent_name=os.getenv("LIVEKIT_AGENT_NAME") or "simli-agent")
async def entrypoint(ctx: JobContext) -> None:
    # Speech-to-text, LLM and text-to-speech, all on LiveKit Inference.
    session = AgentSession(
        stt=inference.STT(model="deepgram/nova-3", language="en"),
        llm=inference.LLM(model="google/gemma-4-31b-it"),
        tts=inference.TTS(model="cartesia/sonic-3.5", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"),  # Jacqueline
        # LiveKit's turn detector decides when the user has finished speaking.
        turn_handling=TurnHandlingOptions(turn_detection=inference.TurnDetector()),
    )

    # Connect first: wait_for_join() below only waits once the room is connected.
    await ctx.connect()

    avatar = simli.AvatarSession(
        simli_config=simli.SimliConfig(
            api_key=os.environ["SIMLI_API_KEY"],
            face_id=os.getenv("SIMLI_FACE_ID") or "14c60d62-6ebf-424d-b626-1f4f91c33b04",
            max_session_length=600,  # seconds; the avatar leaves after this
            max_idle_time=300,  # seconds without speaking before the avatar leaves
        ),
    )
    await avatar.start(session, room=ctx.room)
    # If Simli refuses the session (a bad key or face ID, or no free session), the plugin
    # logs the error and returns without connecting the agent's voice to the avatar.
    if session.output.audio is None:
        ctx.delete_room()
        return

    # Don't start talking until the avatar is in the room, so its first words aren't lost.
    try:
        await avatar.wait_for_join(timeout=30)
    except asyncio.TimeoutError:
        ctx.delete_room()
        return

    await session.start(agent=Agent(instructions="You are a friendly voice assistant."), room=ctx.room)
    session.say("Hi, I'm Ava. What would you like to talk about?")


if __name__ == "__main__":
    cli.run_app(server)
```

The order matters: connect, start the avatar, wait for it to join, and only then start the session and greet. Audio the agent sends before the avatar is ready is lost.

### Trinity-2 options

This gives a Trinity-2 face Simli's default 768x768 video. From `livekit-plugins-simli` 1.8.10, `SimliConfig` also takes [Trinity-2's options](https://docs.simli.com/api-reference/trinity-2.md#framing-the-video), and sends them only when you set them. For a 16:9 video with the avatar at the bottom centre:

```python theme={null}
simli.SimliConfig(
    api_key=os.environ["SIMLI_API_KEY"],
    face_id=os.getenv("SIMLI_FACE_ID") or "14c60d62-6ebf-424d-b626-1f4f91c33b04",
    max_session_length=600,
    max_idle_time=300,
    streaming_width=1366,  # 1:1, 16:9 or 9:16, each side at least 512
    streaming_height=768,
    avatar_position_x=0.5,  # 0 (left) to 1 (right)
    avatar_position_y=1.0,  # 0 (top) to 1 (bottom)
    # camera={"zoom": 1.2},          # a starting camera
    # background_id="<upload id>",   # your own background
)
```

The fields are `streaming_width`, `streaming_height`, `avatar_position_x`, `avatar_position_y`, [`camera`](https://docs.simli.com/api-reference/trinity-2/camera), and [`background_id` and `foreground_id`](https://docs.simli.com/api-reference/trinity-2/backgrounds). Set `max_idle_time` yourself: the plugin's default is 30 seconds. On an older plugin, upgrade, or see [Trinity-2 in LiveKit Agents and Pipecat](https://docs.simli.com/api-reference/trinity-2/integration-workarounds.md).

### 4. Run it

```bash theme={null}
python agent.py dev     # while developing, with debug logs
python agent.py start   # in production
```

Both connect to your LiveKit project and wait. The agent joins a room, and starts a Simli session, when the room's token [dispatches it by name](https://docs.livekit.io/agents/server/agent-dispatch/): `simli-agent`, or your `LIVEKIT_AGENT_NAME`.

`python agent.py console` talks to the agent in your terminal instead. It's voice only, with no avatar, since there's no LiveKit room, but it still needs the `LIVEKIT_*` values for LiveKit Inference. The starter's agent handles that case. The [LiveKit CLI](https://docs.livekit.io/agents/server/startup-modes/) can also run the same `agent.py`, with `lk agent dev` and `lk agent console`.

### 5. Talk to it

Use the starter's web page, LiveKit's [Agents Playground](https://agents-playground.livekit.io) with **Agent name** set to `simli-agent`, or any LiveKit agent frontend whose token dispatches that name, such as [agent-starter-react](https://github.com/livekit-examples/agent-starter-react). The avatar's video arrives as the agent's video.

## Troubleshooting

* **The Simli avatar didn't start:** the Simli error logged just before says why. `401` means `SIMLI_API_KEY` is wrong, `400` means the face ID wasn't found, and `429` means your Simli account is already running as many sessions as it can.
* **The Simli avatar didn't join the room:** usually Simli couldn't reach your LiveKit server. `LIVEKIT_URL` must be reachable from the internet, not `localhost`.
* **The avatar appears but never answers, and the agent logs STT, LLM or TTS errors:** LiveKit Inference refused the request. Check that `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` belong to the project in `LIVEKIT_URL`, and whether this month's Inference credit is used up.
* **The page says the avatar didn't join:** check that the agent is running, that `.env` and `frontend/.env.local` hold the same `LIVEKIT_*` values, and that `LIVEKIT_AGENT_NAME` matches in both if you set it.

## Before you go to production

* Anyone who can join a room in your LiveKit project starts a conversation, which uses your Simli minutes and LiveKit Inference credit. The starter page's token route has no sign-in, so a production build refuses to hand out tokens (403) until you add your own sign-in, or set `ALLOW_UNAUTHENTICATED_SESSIONS=true` for a demo.
* The avatar leaves the room as soon as its agent does, so ending the agent's session, or deleting the room, ends the Simli session and its billing. A brief network blip on the agent's side doesn't count: LiveKit resumes the connection.
* To deploy the agent, run `python agent.py start` on any server, or see [LiveKit's deployment guide](https://docs.livekit.io/deploy/agents/). The page builds with `npm run build` and runs with `npm start`, or deploys to any Next.js host.

## Learn more

* The starter's [README](https://github.com/simliai/create-simli-app-livekit-agents#readme): changing the face, models, voice and prompt, and the free plans' limits
* [LiveKit's guide to Simli](https://docs.livekit.io/agents/models/avatar/plugins/simli/)
* [LiveKit Agents docs](https://docs.livekit.io/agents/): tools, other models and more


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.