egma package adds two functions to your LiveKit worker:
monitorsends production traces to Monitoring.simulationreports the agent’s tools, installs the test’s mock responses, and sends the agent’s trace to the simulation record.
simulation for every LiveKit simulation, including tests without mocks. Use monitor for production monitoring. You can call both in the same worker; each acts only in its own kind of room. First connect the worker through LiveKit.
Install
Use Python 3.11 or newer andlivekit-agents>=1.6.6,<1.9.
openai>=2,<3; let your package manager resolve that range with your LiveKit dependencies.
Configure Egma
Both functions need an Egma URL and a project-scoped API key. Create a key in Settings → API keys, or runegma project api-key create --name "LiveKit worker" from an initialized agent repository.
Set these values where the worker runs:
Keep the key in your deployment’s secret store. The worker must be able to reach the URL. In a container or on another machine,
localhost refers to that worker, not your Egma server. These settings are required for simulations as well as monitoring.
Add the SDK to your worker
Callmonitor(ctx) at the start of your job entrypoint, before connecting or starting the session. Call await simulation(agent, ctx, session) after creating the agent and session, before session.start.
The following worker uses OpenAI for speech and responses. Install its model plugins alongside the SDK:
LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET, and OPENAI_API_KEY in the worker environment. Set the Egma variables, then save this as agent.py:
receptionist as the worker dispatch name in your Egma LiveKit connection. For your existing agent, keep its models, tools, and startup behavior and add the two SDK calls at the same points.
Monitor production
Use the configured Egma URL and project key, deploy the worker, and make a production call. Open Monitoring in the key’s project to see the conversation. There is no additional monitoring switch to enable for LiveKit. The helper adds an OTLP/HTTP protobuf exporter to/v1/traces. It keeps compatible existing OpenTelemetry exporters, including LiveKit Cloud observability. It sends batches and flushes the last batch when the job stops.
Rooms whose names start with egma-sim- are reserved for Egma simulations. monitor skips them; simulation sends their traces to the simulation record. Use another prefix for production rooms.
If no conversation appears
Check that you deployed the monitoring call, supplied a project-scoped key with write access, and used a URL reachable from the worker. An organization-wide key cannot ingest traces. The helper raises an error for missing settings, malformed settings, or an incompatible tracer provider. For network or export failures, inspect the worker’s OpenTelemetry logs. Egma can show a conversation only after its trace arrives. Restart the worker after changingEGMA_URL or EGMA_API_KEY.
Mock tool responses
Add a mock to a test using the exact registered tool name. For the worker above:answer or error for that tool in that test. simulation connects to the simulation room if necessary, finds Egma’s participant, and installs only the selected mocks. It follows agent handoffs and agent tasks in the same session. Call it once for the initial agent.
The function also exports the agent’s own conversation trace over OTLP, even when the test has no mocks. The simulation transcript includes the agent’s turns and tool calls, including real tools. Calls answered by a mock are marked mocked.
Simulation spans are batched every second and flushed when the session closes and when the job stops. Keep one LiveKit job per process: the exporter is bound to that job’s room, and a second job in the same process is refused.
The helper has no total startup deadline. It waits while the simulation room is active; individual token requests and hello attempts keep their transport timeouts. A permanent refusal, room disconnect, Egma participant departure, or cancellation ends startup. Do not catch
NotReported and start the session anyway. Check the room connection, Egma’s participant, and the SDK version when this error occurs. Missing settings or an incompatible tracing setup raise ValueError instead.
Without the SDK report, Egma fails the LiveKit simulation. Check the simulation’s tool calls to confirm the response used. See Mock tool responses.
After your normal session.start returns, LiveKit publishes its native agent state. Egma waits for an initialized state before sending the first simulated input. This wait uses the simulation’s configured duration. You do not need another readiness callback.
When that simulation ends, Egma finishes pending output and its exact participant leaves the room. The SDK closes the AgentSession you supplied, which completes LiveKit’s native session trace, flushes the final evidence, and releases entrypoint code that is waiting for session close. An abrupt room disconnect closes the session as well. These listeners are installed only after the Egma participant accepts the tool report in a simulation room.
Test in chat mode
For a LiveKit chat connection, keep theegma-sim-chat- branch shown in the example. It disables audio input and output and sends text without waiting for speech transcription. Also disable any independent audio publishers in that branch.
Other room names keep your normal voice settings. A worker that still publishes audio in an Egma chat simulation causes the simulation to stop.
Read test startup data
Put data that the worker normally needs at startup in the test’s## Env section:
Function reference
Each function returns before reading settings in the room type it does not handle. The active function still needs
EGMA_URL and EGMA_API_KEY, so a worker used only for simulations needs both settings too.