For clean Markdown of any page, append .md to the page URL. For a complete documentation index, see https://docs.telcoflow.com/use-cases/llms.txt. For full documentation content, see https://docs.telcoflow.com/use-cases/llms-full.txt.

# AI Receptionist with Database Lookup

An AI agent answers the call, looks up the caller in your database, and provides personalized service before routing to the right department.

## Overview

This use case demonstrates:
- Database lookup by caller number on call arrival
- Creating new leads for unknown callers
- Intent classification to determine routing
- Seamless handoff via `connect()` and `close()`

**State flow:** PENDING -> ANSWERED -> CONNECTED -> DISCONNECTED

## Example

```python
import asyncio
from telcoflow_sdk import TelcoflowClient, TelcoflowClientConfig, ActiveCall
import telcoflow_sdk.events as events

@client.on(events.INCOMING_CALL)
async def ai_receptionist(call: ActiveCall):
    await call.answer()

    # Look up caller in your CRM / database
    customer = await db.get_customer_by_phone(call.caller_number)

    if customer:
        # Personalized greeting
        greeting = f"Hello {customer.name}, welcome back."
        await call.send_audio(await tts.synthesize(greeting))

        # Check for open tickets, pending orders, etc.
        open_tickets = await db.get_open_tickets(customer.id)
        if open_tickets:
            msg = f"I see you have {len(open_tickets)} open support tickets."
            await call.send_audio(await tts.synthesize(msg))
    else:
        # Log new caller to database
        await db.create_lead(phone=call.caller_number, source="inbound_call")
        await call.send_audio(await tts.synthesize("Welcome! How can I help you?"))

    # AI listens, determines intent, and decides next action
    should_connect = await ai_model.classify_intent(call)

    if should_connect:
        await call.send_audio(
            await tts.synthesize("Let me connect you now.")
        )
        # Connect to the original callee and leave the call
        await call.connect()
        await call.close()
    else:
        # Continue the AI conversation
        async for chunk in call.audio_stream():
            response = await ai_model.generate(chunk)
            await call.send_audio(response)
```

## How It Works

1. The call arrives and the AI agent answers immediately
2. The agent uses `call.caller_number` to look up the caller in a CRM or database
3. If found, the agent delivers a personalized greeting and surfaces relevant account info (open tickets, pending orders, etc.)
4. If not found, the agent creates a new lead record in the database
5. The AI listens to the caller and classifies their intent
6. If the caller needs to speak to a human, the AI connects them to the original callee using `connect()` and then leaves the call with `close()`
7. If the intent doesn't require a human, the AI continues the conversation directly

## Key Commands Used

- [`answer()`](/concepts/call-commands#answer) - Answer the incoming call
- [`send_audio()`](/concepts/audio-streaming#sending-audio) - Play synthesized speech to the caller
- [`connect()`](/concepts/call-commands#connectring_time_seconds60) - Connect to the original callee
- [`close()`](/concepts/call-commands#close) - Leave the call after connecting the caller to the callee

## Related

- [AI Agent with Human Escalation](/use-cases/human-escalation) - Similar pattern with intelligent escalation
- [After-hours Voicemail](/use-cases/after-hours-voicemail) - Time-based routing before answering