# Voice Agent LiveKit

AI voice agent using LiveKit and ElevenLabs for real-time conversations.

## Overview

This project implements a Python-based voice agent that uses:
- **LiveKit** for real-time audio streaming and room management
- **ElevenLabs** for natural-sounding text-to-speech
- **OpenAI** for conversation intelligence

## Development

### Prerequisites

- Python 3.11 or higher
- uv package manager (installed via npm/pnpm)

### Setup

Install dependencies:

```bash
pnpm nx run voice-agent-livekit:install
```

This will create a virtual environment at `.venv/` and install all dependencies using `uv sync`.

### Running Tests

```bash
pnpm nx run voice-agent-livekit:test
```

### Linting

```bash
pnpm nx run voice-agent-livekit:lint
```

### Local Railway Testing

Before deploying to Railway, test the deployment locally:

```bash
./test-railway-local.sh
```

This script:
- Checks Python syntax with ruff
- Tests module imports
- Runs the agent for 5 seconds to verify startup
- Analyzes logs for errors
- Confirms the agent starts successfully

If all tests pass, the agent should work on Railway!

## Project Structure

```
voice-agent-livekit/
├── src/
│   └── voice_agent_livekit/
│       └── __init__.py
├── tests/
│   └── __init__.py
├── pyproject.toml        # Python dependencies and project metadata
├── project.json          # Nx project configuration
└── README.md
```

## POC Usage

### Prerequisites

Before running the POC agent, you'll need API keys for:

1. **LiveKit Cloud** - Get from: https://cloud.livekit.io/projects/p_0u4r863k16t/settings/keys
2. **ElevenLabs** - Get from: https://elevenlabs.io/app/settings/api-keys
3. **Deepgram** - Get from: https://console.deepgram.com/
4. **OpenAI** - Get from: https://platform.openai.com/api-keys

### Setup Environment

1. Copy the example environment file:
```bash
cp .env.example .env
```

2. Edit `.env` and fill in your API keys:
```bash
# LiveKit Cloud credentials
LIVEKIT_URL=wss://your-project.livekit.cloud
LIVEKIT_API_KEY=your-api-key
LIVEKIT_API_SECRET=your-api-secret

# Voice service API keys
ELEVENLABS_API_KEY=your-elevenlabs-key
DEEPGRAM_API_KEY=your-deepgram-key
OPENAI_API_KEY=your-openai-key

# Test room (optional - will auto-generate if not set)
LIVEKIT_ROOM_NAME=test-voice-agent
```

### Running the POC

Run the agent using the Nx command:

```bash
pnpm nx run voice-agent-livekit:run-poc
```

Or run directly with uv:

```bash
cd apps/voice-agent-livekit
uv run python -m voice_agent_livekit.poc_agent
```

### Testing with LiveKit Sandbox

1. Start the POC agent (see above)
2. Open LiveKit Sandbox: https://cloud.livekit.io/projects/p_0u4r863k16t/sandbox
3. Join the same room as the agent (check agent logs for room name)
4. Allow microphone access in your browser
5. You should hear the agent speak a greeting
6. Speak into your microphone - the agent will echo back what it hears

### Expected Behavior

When the POC is working correctly:

1. **Agent Joins Room**: Agent connects to LiveKit room and appears in the participant list
2. **Agent Speaks Greeting**: You hear a clear voice greeting using ElevenLabs TTS
3. **Agent Listens**: When you speak, the agent transcribes using Deepgram STT
4. **Agent Responds**: The agent acknowledges what you said and responds naturally

### Voice Quality Assessment

During testing, evaluate:

- **Clarity**: Is the voice clear and natural-sounding? (Target: 4+ out of 5)
- **Latency**: Time from when you stop speaking to when agent responds (Target: <3 seconds)
- **Accuracy**: Does the agent correctly transcribe what you say? (Target: >90%)

### Troubleshooting

**Agent doesn't connect:**
- Check that all API keys are set in `.env`
- Verify LiveKit URL and credentials are correct
- Check agent logs for error messages

**No audio from agent:**
- Verify ELEVENLABS_API_KEY is valid
- Check browser console for audio playback errors
- Ensure speakers are not muted

**Agent doesn't hear you:**
- Allow microphone permissions in browser
- Verify DEEPGRAM_API_KEY is valid
- Check agent logs for STT errors

**High latency:**
- This is a POC with no optimizations
- Production version (Task #144) will include latency improvements

### Cost Tracking

Approximate costs per session:
- ElevenLabs TTS: ~$0.24 per session (based on character count)
- Deepgram STT: ~$0.022 per session (based on audio duration)
- OpenAI GPT-4o-mini: ~$0.10 per session (based on tokens)
- **Total**: ~$0.36 per session

### Next Steps

After validating the POC:
- Document findings in Stream C (Architecture Documentation)
- Test edge cases in Stream D (Testing & Validation)
- If successful, proceed to Task #144 (Build Voice Agent Application)
