How does a voice agent manage conversation state across a long call?

Authors
How does a voice agent manage conversation state across a long call?

A voice agent manages conversation state across a long call by keeping a small set of important facts in a structured state, keeping only the recent turns word by word, and turning older turns into a short summary, so that the LLM always gets what it needs without the history growing forever.

In this blog, we will learn about how a voice agent manages conversation state across a long call. We will also see what conversation state is, why a long call breaks the simple approach, how the agent trims and summarizes old turns, how it keeps facts in a structured state, how it tracks the stage of the call, and how it keeps the state safe when the call drops.

We will cover the following:

  • What is conversation state?
  • How does the simple approach work?
  • Why does a long call break the simple approach?
  • Approach 1: Keep only the recent turns
  • Approach 2: Summarize the older turns
  • Approach 3: Keep the important facts in a structured state
  • Tracking the stage of the call
  • Keeping tool results small
  • Saving only what the user actually heard
  • Keeping the state safe when the call drops
  • Putting it all together

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 is conversation state?

Conversation state is everything the voice agent must remember about the call so far, so that its next reply makes sense.

It includes:

  • What the user has said.
  • What the agent has said.
  • The facts collected so far, like the name, the patient ID, and the date of the appointment.
  • The results of any tool calls, like the free slots fetched from the clinic calendar.
  • The stage of the call, like "verify" (checking who the caller is) or "booking" (finding a new slot).

Let's say we call a clinic and talk to a voice agent. In the first minute, we tell it our name. In the fifth minute, we ask to move our appointment. The agent must still know our name in the fifth minute. That memory is the conversation state.

Here, one thing we must understand is that the LLM (Large Language Model) inside the agent does not remember anything on its own between two requests. Every time the agent needs a reply, our code sends the LLM the full picture again. So, the conversation state lives in our code, not inside the LLM.

How does the simple approach work?

The simplest way is to keep a list of messages. Every time the user speaks, we add the user's words to the list. Every time the agent replies, we add the reply to the list. Then, for every new reply, we send the whole list to the LLM.

The list looks like below:

messages = [
    { "role": "system", "content": "You are a helpful clinic assistant." },
    { "role": "user", "content": "Hi, my name is Riya Sharma." },
    { "role": "assistant", "content": "Hi Riya, how can I help you today?" },
    { "role": "user", "content": "I want to move my appointment." },
]

Here, we have:

  • A system message that tells the LLM how to behave.
  • user messages that hold what the caller said, after the speech-to-text (STT) step turned the audio into text.
  • assistant messages that hold what the agent said.

For a short call of two or three minutes, this works perfectly.

Why does a long call break the simple approach?

Now, let's say the call goes on for 30 minutes. The list keeps growing with every turn.

But the following issues arise:

  • The context window fills up. Every LLM can read only a limited number of tokens in one request. This limit is called the context window. A very long call can come close to it, especially when tool results are large.
  • Every reply gets slower. The LLM must read the whole list before it starts to answer. A longer list usually means more time before the first word comes out.
  • Every reply costs more. We pay for the tokens we send. Sending the full history on every turn means we pay for the same old turns again and again.
  • The LLM loses focus. When important facts are buried deep inside a long history, the LLM can miss them. It can overlook the name the user gave 25 minutes ago or mix up two appointment dates.

In a chat app, a small delay is fine. In a voice call, even one extra second of silence feels broken. Hence, a growing history hurts a voice agent more than a chat app.

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.

Approach 1: Keep only the recent turns

The first idea is to keep only the last few turns and drop the older ones. This is called a sliding window.

Let's say we keep only the last 10 messages. When the 11th message comes, we remove the oldest one. We can write the code as below:

MAX_MESSAGES = 10

def add_message(messages, new_message):
    messages.append(new_message)
    # keep the system message, plus the last MAX_MESSAGES messages
    system = messages[0]
    recent = messages[1:][-MAX_MESSAGES:]
    return [system] + recent

Here, we always keep the system message at the top, because it tells the LLM how to behave. Then, we keep only the last 10 messages of the conversation.

Advantage: The list never grows beyond a fixed size, so the speed and the cost stay steady for the whole call.

Disadvantage: The agent forgets everything that fell out of the window. If the user said their name in the first minute, the agent no longer knows it in the twentieth minute.

The issue with this approach is that important facts get dropped along with the small talk. Let's see how the next approach solve this issue.

Approach 2: Summarize the older turns

The next idea is that, instead of throwing away the old turns, we turn them into a short summary.

Here is how it works:

  • We keep the recent turns word by word.
  • When the older part of the history grows beyond a limit, we ask an LLM to write a short summary of it.
  • We replace those older turns with the summary.

After a few summaries, the list looks like below:

messages = [
    { "role": "system", "content": "You are a helpful clinic assistant." },
    { "role": "system", "content": "Summary so far: The caller is Riya Sharma. "
        "She wants to move her appointment with Dr. Mehta from Monday to a later day. "
        "She prefers mornings." },
    # the last few turns, word by word
    { "role": "user", "content": "Is Thursday possible?" },
]

Here, we can see that the summary carries the important points of the first 20 minutes in a few lines. The recent turns are still kept word by word, so the agent knows exactly what was said just now. We have a detailed blog on how context compaction works that explains this in depth.

One more thing to notice: summarizing takes time, because it is one more LLM request. So, a good voice agent does not summarize while the user is waiting for a reply. It writes the summary in the background, and the later replies use it once it is ready.

Advantage: The history stays small, and the agent still remembers the main points of the whole call.

Disadvantage: A summary can lose exact details. A patient ID like 48213 can turn into "her patient ID", or a date can get slightly changed. Often, each new summary is built from the old summary plus the newer turns, so small mistakes can add up over a long call.

The issue with this approach is that exact facts are not safe inside free text. Let's see how the next approach solve this issue.

Approach 3: Keep the important facts in a structured state

So, here comes the structured state to the rescue.

Instead of hoping that the facts survive inside the history or the summary, we pull them out and keep them in a separate, fixed place. This place is the structured state. Each named fact in it is called a slot.

For example:

state = {
    "caller_name": "Riya Sharma",
    "patient_id": "48213",
    "verified": True,
    "doctor": "Dr. Mehta",
    "current_appointment": "Monday 10:00 AM",
    "requested_day": "Thursday",
    "preferred_time": "morning",
}

Here, we have each important fact stored with its own name. The name, the patient ID, the doctor, and the dates are kept exactly as they are.

Now, the question is: how do these slots get filled? One common way is tool calls. We give the LLM a tool like update_state. When the user says something important, the LLM recommends calling this tool with the new value, and our code runs it and saves the value in the state.

def update_state(state, field, value):
    # our code runs this when the LLM recommends it
    state[field] = value
    return state

Here, the LLM does not change the state by itself. It only recommends the tool call and its input. Our agent code runs the function and updates the state, so the control stays with our code.

Then, on every turn, our code puts the current state at the top of the request, like below:

def build_request(system_prompt, state, summary, recent_turns):
    state_text = "Known facts about this call: " + str(state)
    return (
        [{ "role": "system", "content": system_prompt }]
        + [{ "role": "system", "content": state_text }]
        + [{ "role": "system", "content": "Summary so far: " + summary }]
        + recent_turns
    )

Here, we can notice that every request now has four parts: the instructions, the known facts, the summary of older turns, and the recent turns word by word. The facts are always in the same place, near the top, so the LLM finds them easily.

Advantage: Exact facts do not get lost or rewritten by a summary, no matter how long the call goes on.

Disadvantage: We must decide the slots ahead of time and add a way to fill them, like a tool. Anything that is not a slot still depends on the summary and the recent turns.

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.

Tracking the stage of the call

Now, let's discuss one more part of the state: the stage of the call.

Most business calls follow a fixed set of steps. For example, a clinic call goes like this: greet the caller, verify who they are, understand the request, book the new slot, and confirm. Each step is a stage.

We keep the current stage in the state as below:

state["stage"] = "booking"

Then, our code gives the LLM only the instructions and tools that fit the current stage. In the "verify" stage, the LLM gets the tool to check the caller's date of birth. In the "booking" stage, it gets the tools to find free slots and book one. When one stage is done, our code moves the call to the next stage.

This helps a long call in the following ways:

  • The instructions for each stage are short, so the request stays small.
  • The LLM has fewer tools to choose from, so it picks the right one more often.
  • The agent does not skip a step. For example, it cannot book a slot before it verifies the caller, because the booking tool is not available yet.

When a stage is finished, the detailed turns of that stage are usually not needed anymore. Only the result matters, like "verified": True. So, moving to a new stage is also a natural point to summarize the old turns and keep only the result in the state.

Keeping tool results small

Tool results are often one of the biggest reasons the history grows fast.

Let's say the agent calls a tool to fetch the caller's past appointments. The clinic system returns a long response with 40 fields for each appointment. If we put the whole response into the history, it takes up a large part of the context window, and it stays there for the rest of the call.

So, a good voice agent does the following:

  • It keeps only the fields that the conversation needs, like the date, the time, and the doctor.
  • It saves the useful values into the structured state.
  • It removes or shortens the full tool result from the history once the agent has used it.

We have a complete program that covers Context Engineering, Memory in Agents, Tool use in Agents, and Design a Real-Time Voice AI Agent - check out our AI and Machine Learning Program at Outcome School.

Saving only what the user actually heard

A voice call has one problem that a chat does not have: the user can interrupt the agent in the middle of a sentence.

Let's say the agent's planned reply was: "Your appointment is on Monday at 10 AM, and you can also bring your old reports." The user interrupted right after "Monday at 10 AM". So, the user never heard the part about the reports.

If we save the full planned reply in the history, the agent will believe it already told the user about the reports. Later, it will not repeat it, and the user will never get that information.

So, when an interruption happens, the agent saves only the part of the reply that was actually played to the user. It cuts the assistant message at the point where the audio stopped.

def on_interrupted(messages, spoken_text):
    # the last message is the reply that got interrupted
    # replace it with only the part the user heard
    messages[-1]["content"] = spoken_text

Here, spoken_text is the part of the reply that the user heard before interrupting. The agent can find it using the word timings that many text-to-speech services send along with the audio. Now, the history matches what really happened on the call.

We have a detailed blog on how a voice agent truncates its memory to only what the user actually heard that explains this step by step.

Without this, a long call with many interruptions slowly fills the history with things the user never heard.

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

Keeping the state safe when the call drops

Phone calls can drop. The network can fail, or the server running the agent can restart. If the state lives only in the memory of one running program, a dropped call loses everything.

So, a good voice agent saves the state outside the running program, in a fast storage like a database or a key-value store, after every turn or after every important change.

The saved record usually has:

  • The structured state with the slots and the current stage.
  • The summary of the older turns.
  • The recent turns word by word.
  • A call ID and the caller's phone number, so that the record can be found again.

Now, if the caller calls back two minutes later, the agent can look up the saved record using the phone number and continue from where it left off. It can say, "Welcome back, Riya. We were finding a Thursday morning slot with Dr. Mehta." The same saved record is also useful when the agent transfers the call to a human, because the human gets the full picture without asking the caller to repeat everything.

Putting it all together

Now, let's see how all these pieces work together on every turn of a long call.

User speaks
    ↓
Speech-to-text turns the audio into text
    ↓
Our code adds the user's text to the recent turns
    ↓
Our code builds the request:
instructions for the current stage
+ structured state
+ summary of older turns
+ recent turns
    ↓
LLM recommends a tool call or writes a reply
    ↓
If it is a tool call, our code runs it, updates the
structured state, and asks the LLM for the reply
    ↓
Text-to-speech plays the reply
    ↓
Our code keeps only the part of the reply the user heard
    ↓
Our code saves the state to storage
    ↓
In the background: if the old turns are too long, summarize them

Here, we can see that the request sent to the LLM stays small and does not keep growing with every turn, whether the call is 10 minutes long or 40 minutes long.

In practice, a voice agent built for long calls often uses all three together: a sliding window for the recent turns, a summary for the older turns, and a structured state for the facts that must stay exact.

Now we must have understood how a voice agent manages conversation state across a long call, how it keeps the important facts exact, and how it keeps every reply fast even when the call goes on for a long time.

Frequently Asked Questions

Does the LLM in a voice agent remember the earlier part of the call by itself?

No. The LLM does not remember anything on its own between two requests. Every time the agent needs a reply, our code sends the LLM the full picture again. So, the conversation state lives in our code, not inside the LLM, and our code decides what goes into each request.

Why not just send the full history to the LLM on every turn?

Because a long call makes the full history grow with every turn. It can come close to the context window, every reply gets slower and costs more, and the LLM can miss important facts buried deep inside the history. In a voice call, even one extra second of silence feels broken.

Does summarizing the old turns make the voice agent slower?

Not if it is done in the background. Summarizing is one more LLM request, so it takes time. A good voice agent does not summarize while the user is waiting for a reply. It writes the summary in the background, and the later replies use it once it is ready.

Why should exact values like a patient ID not live only in the summary?

Because a summary can lose exact details. A patient ID like 48213 can turn into "her patient ID", or a date can get slightly changed, and small mistakes can add up over a long call. Keeping such values as slots in a structured state keeps them exact, no matter how long the call goes on.

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.