How does a voice agent decide when to end the call?

Authors
How does a voice agent decide when to end the call?

A voice agent decides when to end the call by watching for a few signals, like the user hanging up, the LLM recommending an end to the call once the conversation is complete, the user staying silent for too long, or the call hitting a time limit. When the agent itself ends the call, it says a short goodbye, waits for that goodbye to finish playing, and then hangs up.

In this blog, we will learn about how a voice agent decides when to end the call. We will also see why ending a call is harder than it looks, how the agent reacts when the user hangs up first, how the LLM recommends ending the call with an end call tool, how the agent says goodbye without cutting it off, how silence and time limits end a call, and how we stop the agent from hanging up too early.

We will cover the following:

  • What does ending a call mean for a voice agent?
  • Why is ending a call hard?
  • The signals that end a call
  • Signal 1: The user hangs up
  • Signal 2: The LLM recommends ending the call
  • Saying goodbye without cutting it off
  • Signal 3: The user goes silent
  • Signal 4: The call hits a time limit
  • Signal 5: The call is transferred or reaches a voicemail
  • Stopping the agent from hanging up too early
  • What happens after the call ends
  • Putting it all together in code

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What does ending a call mean for a voice agent?

Many voice agents work as a pipeline of three steps: Speech-to-Text (STT) turns what the user said into text, an LLM (Large Language Model) decides the reply, and Text-to-Speech (TTS) turns the reply into a voice.

The end of a call is the moment when the line is closed. After that, no audio goes in and no audio comes out. On a phone call, this is the same as pressing the red button on our phone.

Ending a call means the agent decides that the conversation is over, finishes what it is saying, and closes the line.

When two people talk on the phone, they say "Okay, bye", and one of them hangs up. But, a voice agent must be told exactly when to hang up and how to do it.

Why is ending a call hard?

Let's say we are building a voice agent for a restaurant that books tables. It can make three mistakes at the end of a call.

Mistake 1: It hangs up too early. The user says, "Thank you," and pauses to think. The agent thinks the call is over and hangs up. But, the user was about to say, "Thank you, and can I also bring my dog?" Now, the user is angry and has to call again.

Mistake 2: It never hangs up. The booking is done. The user says "Bye" and walks away without pressing the red button. The agent keeps the line open and keeps talking to nobody. Every minute on the line costs money for the phone service, the STT, the LLM, and the TTS.

Mistake 3: It cuts its own goodbye. The agent starts saying, "Your table is booked. Have a great evening." But, it closes the line after "Your table is". The user hears half a sentence and a click.

The signals that end a call

A voice agent ends the call when one of a few signals comes. The signals are:

  • The user hangs up.
  • The LLM recommends ending the call because the conversation is complete.
  • The user goes silent for too long.
  • The call hits a time limit.
  • The call is transferred to a human or reaches a voicemail.

We will learn about each of them in detail.

Signal 1: The user hangs up

This is the easiest signal, because the user decides for us.

On a phone call, when the user presses the red button, the phone network sends a message to the other side that the call is over. In SIP (Session Initiation Protocol), the protocol that carries most internet phone calls, this message is called BYE. In an app or a website, the connection between the user and the agent closes.

The agent does not need to decide anything here. It needs to react quickly: stop the LLM and the TTS, stop sending audio, and run the cleanup work. Otherwise, it keeps paying for the LLM and the TTS to talk to nobody.

Signal 2: The LLM recommends ending the call

Most of the time, we want the agent itself to decide that the conversation is over. This is the hardest signal.

The naive approach is to look for words. If the user says "bye" or "thank you", we hang up.

The issue with this approach is that words alone do not tell us the meaning. Let's see a few examples:

  • "Thank you, and one more thing." The user is not done.
  • "Okay, bye. Oh wait, what time do you close?" The user is not done.
  • "Okay, that is all." The user is done, but there is no "bye" or "thank you" in it.

Let's see how the next approach solve this issue.

The better approach is to let the LLM decide, because the LLM understands the whole conversation, not only the last word.

An LLM can use function calling. We describe a function to the LLM, and when that function is needed, the LLM replies with its name instead of a normal text reply. The LLM never runs the function itself. It recommends it, and our code runs it.

So, we give the LLM a function called end_call, and we can describe it as below:

{
  "type": "function",
  "name": "end_call",
  "description": "End the phone call. Use this only when the user's request is fully handled and the user has clearly said goodbye or said they need nothing else.",
  "parameters": {
    "type": "object",
    "properties": {
      "reason": {
        "type": "string",
        "description": "Why the call is ending, for example: booking_done or user_said_goodbye"
      }
    },
    "required": ["reason"]
  }
}

Here, we can see that the description gives two conditions that must both be true: the request is fully handled, and the user has clearly said they are done. The reason field helps us later, when we look at why our calls ended.

We also add a few lines to the system prompt, which is the set of instructions the LLM reads before every reply:

When the booking is done, ask if the user needs anything else.
If the user says no or says goodbye, say a short goodbye and then use the end_call tool.
Never use end_call while the user is still asking for something.

Now, let's see the restaurant call again:

  • User: "Book a table for two at 8 PM."
  • Agent: "Done. Your table for two is booked at 8 PM. Is there anything else I can help you with?"
  • User: "No, that is all, thanks."
  • Agent: "Have a great evening. Goodbye." Along with this reply, the LLM recommends end_call with the reason booking_done.

The LLM read the full conversation. It saw that the booking was done and that the user said no to "anything else?". So, it recommended ending the call.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

Saying goodbye without cutting it off

Now, the next big question is: when exactly should our code close the line?

The LLM recommended end_call and wrote "Have a great evening. Goodbye." But, these words are not spoken yet. They still have to become audio in the TTS, travel over the network, and play on the user's phone. This takes a few seconds.

If our code closes the line the moment it sees end_call, the user hears only "Have a great" and then a click. This is Mistake 3 from earlier.

So, the rule is: close the line only after the goodbye audio has finished playing.

The flow looks like below:

+----------------------------------+
| LLM writes "Goodbye"             |
| and recommends end_call          |
+----------------------------------+
                  ↓
+----------------------------------+
| TTS turns "Goodbye" into audio   |
+----------------------------------+
                  ↓
+----------------------------------+
| Audio plays to the user          |
+----------------------------------+
                  ↓
+----------------------------------+
| Agent waits until playback       |
| is finished                      |
+----------------------------------+
                  ↓
+----------------------------------+
| Agent closes the line            |
+----------------------------------+

Here, the important step is the wait. Our code does not close the line right away. It marks the call as "ending", lets the goodbye play fully, and only then closes the line. We can also add a short pause, around half a second, after the audio ends, so that the last word is not clipped.

One more thing to notice: in normal function calling, our code sends the result of a function back to the LLM, and the LLM writes another reply. For end_call, we do not want that, because the LLM can then say "The call has ended." to the user. So, end_call does not trigger another reply.

Also, the user can still talk while the goodbye is playing. If the user says, "Wait, one more question", the agent must stop the goodbye, cancel the ending, and go back to the conversation, just like any other interruption.

Signal 3: The user goes silent

Now, let's handle Mistake 2, where the agent never hangs up.

Sometimes, the user walks away, or the network drops their audio. The line is still open, but nobody is there.

For this, the agent uses a silence timeout. It is a timer that starts every time the user stops speaking. If the user speaks again, the timer resets. If the timer runs out, the agent acts.

A good agent does not hang up at the first silence. It follows the steps like below:

  • Step 1: The user is silent for around 10 seconds. The agent asks, "Are you still there?"
  • Step 2: The user is still silent for another 10 seconds. The agent says, "I am not able to hear you. Please call again anytime."
  • Step 3: The agent waits for this message to finish playing and closes the line.

The exact numbers depend on our use case. A call where the user reads a long card number needs a longer timeout than a quick booking call.

Silence must be measured from the user's side only. When the agent is talking, or waiting for a slow tool like a booking system, the timer must not count those seconds. Otherwise, the agent will ask "Are you still there?" while it is the one keeping the user waiting.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Signal 4: The call hits a time limit

Even a healthy call must not go on forever. So, we set a maximum call duration, for example 15 or 30 minutes, based on our use case.

This protects us from a call stuck in a loop, a call where another machine on the other side keeps talking to our agent, and a very large bill from one single call.

When the limit is close, the agent gives a warning first, for example one minute before: "We are almost out of time." Then, at the limit, it says goodbye, waits for the audio to finish, and closes the line.

Signal 5: The call is transferred or reaches a voicemail

Sometimes, the agent is not the one who finishes the conversation:

  • Transfer to a human: The user asks for a person, or the agent cannot help. The agent transfers the call to a human. After the transfer is done, the agent leaves the call, even though the call continues with the human.
  • Voicemail: In an outbound call, where the agent dials, the call can reach an answering machine. The agent detects this, leaves a short message or none, and hangs up.

In both cases, the decision to leave comes from the task itself, not from a goodbye. The transfer is often built as a tool too, like transfer_call, which the LLM recommends just like end_call.

Stopping the agent from hanging up too early

Now, let's handle Mistake 1. Hanging up too early is worse than hanging up late, because the user loses their work and has to call again.

We can use a few guards:

  • Ask before ending. The agent asks "Is there anything else I can help you with?" before it ends the call. It ends only after the user says no. This one question prevents many early hang-ups.
  • Write a strict tool description. The end_call description must say clearly when to use it and when not to use it. A vague description like "End the call" can make the LLM recommend it too early.
  • Check the state in our code. Our code can check if important work is finished before it accepts end_call. For example, if a booking was started but not saved, our code refuses to hang up and tells the LLM to finish the booking first.
  • Let the user cancel the ending. As we saw earlier, if the user speaks during the goodbye, the agent cancels the ending.
  • Test with many conversations. We run test calls and count how often the agent hung up too early or never hung up, then improve the prompt and the tool description.

Here, we can notice that the LLM recommends, but our code has the final say, because hanging up cannot be undone.

To learn Tool use in Agents, Evaluation of LLMs and Agents, and how to Design a Real-Time Voice AI Agent, check out our AI and Machine Learning Program at Outcome School.

What happens after the call ends

The line is closed, but the agent still runs the cleanup steps:

  • Stop all timers, the STT, the LLM, and the TTS, so that we do not keep paying for them.
  • Save the full transcript of the call.
  • Save the reason why the call ended, like booking_done, user_hung_up, silence_timeout, or max_duration.
  • Send any follow-up, like a booking confirmation by text message.

The saved reason helps us find problems. Let's say 20 percent of our calls end with silence_timeout. This tells us that something is wrong, like confusing questions or user audio that is not reaching the agent.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

Putting it all together in code

Now, let's see the code for an agent that handles all these signals. For the sake of understanding, this is a simple Python version, and helpers like speak and hang_up stand for the real parts of our voice pipeline.

import asyncio

SILENCE_LIMIT = 10        # seconds before "Are you still there?"
MAX_CALL_SECONDS = 900    # 15 minutes

class Call:
    def __init__(self, line):
        self.line = line
        self.ending = False
        self.end_reason = None

    async def end(self, goodbye, reason):
        self.ending = True
        self.end_reason = reason
        await speak(self.line, goodbye)       # returns after playback ends
        await asyncio.sleep(0.5)              # short pause
        if self.ending:                       # not cancelled by the user
            await hang_up(self.line)

    async def on_llm_tool_call(self, name, args, reply_text):
        if name == "end_call":
            if booking_in_progress(self):
                return "Finish the booking before ending the call."
            await self.end(reply_text, args["reason"])
            return None                       # no new LLM reply after end_call

    def on_user_speech(self):
        self.ending = False                   # the user spoke, cancel any ending
        self.end_reason = None

Here, we have:

  • end() speaks the goodbye, waits until the audio has finished playing and a short moment more, and then closes the line.
  • self.ending is the flag that marks the call as "ending". If the user speaks during the goodbye, on_user_speech() sets it back to False, and the agent does not hang up.
  • on_llm_tool_call() runs when the LLM recommends a function. For end_call, it refuses if a booking is still in progress. Otherwise, it ends the call and returns None, so that the LLM does not write another reply.

Now, let's add the silence timer and the time limit:

    async def watch_silence(self):
        while not self.ending:
            await wait_for_user_silence(self.line, SILENCE_LIMIT)
            await speak(self.line, "Are you still there?")
            heard = await wait_for_user_speech(self.line, SILENCE_LIMIT)
            if not heard:
                await self.end("I am not able to hear you. Please call again anytime.", "silence_timeout")

    async def watch_duration(self):
        await asyncio.sleep(MAX_CALL_SECONDS - 60)
        await speak(self.line, "We are almost out of time.")
        await asyncio.sleep(60)
        await self.end("Thank you for calling. Goodbye.", "max_duration")

    async def on_line_closed(self):
        self.ending = True
        self.end_reason = self.end_reason or "user_hung_up"
        stop_pipeline(self.line)
        save_transcript_and_reason(self)

Here, we have:

  • watch_silence() asks "Are you still there?" after 10 seconds of user silence, and ends the call with silence_timeout if there is still no reply. The helper wait_for_user_silence counts only the seconds when the agent is not talking and not waiting on a tool.
  • watch_duration() warns the user one minute before the limit, then ends the call with max_duration.
  • on_line_closed() runs whenever the line closes, no matter who closed it. If the user hung up first, the reason is user_hung_up.

Here, I have kept only the important lines of the code and removed other lines for brevity.

Now we must have understood how a voice agent decides when to end the call, how it says goodbye without cutting it off, and how it avoids hanging up too early.

Frequently Asked Questions

Should a voice agent hang up when the user says "bye"?

No. Words alone do not tell us the meaning. A user can say "Okay, bye. Oh wait, what time do you close?" and still not be done, or say "Okay, that is all." with no "bye" in it. So, we let the LLM decide with an end_call tool, because it understands the whole conversation, not only the last word.

Does the LLM end the call by itself?

No. The LLM never runs the function itself. It recommends end_call, and our code runs it. Our code has the final say, because hanging up cannot be undone. For example, if a booking was started but not saved, our code refuses to hang up and tells the LLM to finish the booking first.

Why does end_call not trigger another LLM reply?

Because the LLM could then say "The call has ended." to the user. In normal function calling, our code sends the result of a function back to the LLM, and the LLM writes another reply. For end_call, we skip this step, so the goodbye is the last thing the user hears.

How long should the silence timeout be in a voice agent?

It depends on our use case. A common pattern is to ask "Are you still there?" after around 10 seconds of user silence, and end the call after another 10 seconds without a reply. A call where the user reads a long card number needs a longer timeout than a quick booking call.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.