How to make OpenAI Realtime API agent end Twilio call programmatically?

I’m working with OpenAI’s Realtime API and Twilio for voice calls. I want the AI agent to be able to end the call when it decides the conversation is over.

Here’s the relevant part of my WebSocket handler:

async def handle_media_stream(websocket: WebSocket):
    async with websockets.connect(
        'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01',
        extra_headers={
            "Authorization": f"Bearer {OPENAI_API_KEY}",
            "OpenAI-Beta": "realtime=v1"
        }
    ) as openai_ws:
        # ... other code ...

        async def send_to_twilio():
            async for openai_message in openai_ws:
                response = json.loads(openai_message)
                if response['type'] == 'response.audio.delta' and response.get('delta'):
                    # handle audio response
                    pass

I tried creating a function to end the call:

async def end_twilio_call(call_sid):
    client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
    call = client.calls(call_sid).update(status="completed")

But I’m not sure:

  1. How to properly detect when the AI wants to end the call
  2. How to get the call_sid in the WebSocket handler
  3. What’s the best way to implement this without breaking the stream

Any help would be appreciated! Has anyone implemented something similar

1 Like

I am too Looking for same thing to end the call when done but not able to yet.

Hey!

I don’t use Twilio but a SIP-Client that needs to end its own call as well.
My logic still applies to your problem though:

Make a function that programmatically ends the call.
Make sure this works when you call it.

Now instead of calling it, you use function calling/tool use for the AI to execute it instead.
In the description of the tool, make sure you describe when and how it should use the tool.

If you can’t figure out how to end a call with the Twilio SDK programmatically, I recommend asking on their forum instead.

Cheers! :hugs:

1 Like