# Conversational Agents

Dialogue systems maintaining coherent multi-turn interaction, tracking state and intent across a conversation.

**Rests on:** Chat Format Templates, Conversation State

**Leads to:** LLM as Study Partner

*AI Agents · AI Agents*


## Why learn this

You have probably used a customer support bot or a voice assistant that forgot what you said three messages ago. That frustration is common because most simple systems only see one message at a time. Understanding how to build a system that remembers the full conversation allows you to create tools that actually help people through complex tasks, like booking a flight where details change between sentences, rather than just answering single questions. This is the essential skill for building any interactive software that needs to keep track of what has already happened.


## The intuition

Imagine you are helping a friend plan a dinner party over text messages. Your friend sends message one: 'I am inviting five people.' You reply, 'Okay, I will buy food for five.' This is a single exchange. Now the conversation continues. Message two: 'Actually, my sister is coming too.' A system without memory would still plan for five people. A system with continuity looks at your first reply and her update, realizing the total is now six, and updates its plan accordingly. The hard part of this technology is not answering the current message in isolation; it is keeping track of the 'state'—the list of guests, the budget, the time agreed upon—as it changes across ten or twenty messages. When a user says 'What did I say earlier?', the system must retrieve that specific fact from the history without confusing it with similar details said later.


## An analogy

Think of a chef in a busy kitchen preparing a complex dish while talking to a waiter. The waiter brings orders: 'Table two needs steak medium-rare.' The chef acknowledges this and starts cooking. Then another order comes: 'And make sure the potatoes are well-done.' The chef does not restart the steak; they update their mental note of what Table Two wants. If the waiter then asks, 'How is the steak going?', the chef checks their current progress on that specific item. The analogy breaks down because a human chef has innate physical coordination and common sense that an AI lacks; the AI must explicitly record every detail in text form to avoid losing track of which instruction belongs to which order.


## Definition

A conversational agent is a program that maintains context by tracking the cumulative information—such as facts, decisions, and intents—in all previous messages within a session, allowing it to respond logically to new input based on what was established earlier.


## How it connects

This concept supports the broader category of AI Agents, which are systems that take actions in the world to achieve goals. While an agent might plan complex steps or use external software tools, the conversational agent provides the 'memory' and 'intent recognition' layer that tells the agent what is currently relevant. It also serves as the interface for Analyst Agents, which are specialized programs that examine data; when a user interacts with such an agent, the conversation history allows the system to ask follow-up questions or refine results based on previous feedback.


## The mathematics

The mathematics of conversational agents is not calculus or algebra — it is probability and information theory, but used lightly. The core operation is: when a user sends a message, the agent must assign a probability to each possible intent — 'the user is ordering food', 'the user is asking for help', 'the user is complaining' — and pick the most likely one. This is a classification problem. The state is a collection of values — what has been ordered, what has been asked — and updating it is a simple assignment: after the user says 'the dosa should be extra spicy', the state variable for spice becomes 'extra'. There is no derivative here; the mathematics is mostly counting and comparing. The key insight is that the agent is not doing arithmetic on numbers but comparing likelihoods: given the words spoken and the history, which intent is most probable? That comparison can be done with a simple score — for each candidate intent, count how many clues in the message support it, add them up, and choose the highest. This is the same logic as a dot product: a weighted sum of evidence. But for a learner who has not met vectors, the plain words are: 'the agent keeps a score for each possible intent, adds points for each word that hints at that intent, and picks the intent with the highest score.'

For each candidate intent i, score_i = sum over each word w in the message of weight(w, i). The predicted intent is argmax_i score_i. The symbols: i is the intent (one of the possible actions); w is a word in the user's message; weight(w, i) is a number, learned from data, that says how strongly word w points to intent i; sum means add all those weights together; argmax means 'pick the i with the largest score'.


## A worked example

```
Suppose a travel booking agent has three possible intents: 'book_flight', 'check_weather', 'cancel_trip'. The user says: 'I need to fly to Delhi tomorrow afternoon.' The agent scores each intent based on words present.

Step 1: book_flight score = +3 ('fly' + 'Delhi' + 'tomorrow')   # Each of the three words is strong evidence for booking a flight.
Step 2: check_weather score = +1 ('tomorrow' is weak evidence)   # 'Tomorrow' matters for weather too, but no word like 'rain' or 'temperature' appears.
Step 3: cancel_trip score = 0 (no words match)   # None of the words suggest cancellation.
Step 4: Highest score = book_flight (3 > 1 > 0)   # The agent chooses the intent with the highest score.

Result: The agent identifies the intent as 'book_flight' with high confidence and proceeds to ask for the departure date and return date — updating its state with the user's destination (Delhi) and travel date (tomorrow afternoon).
```


## From scratch, in Python

```python
# The building blocks: a list keeps things in order; a dictionary looks things up by name.

# A dictionary: intent name -> score, initially zero.
scores = {"book_flight": 0, "check_weather": 0, "cancel_trip": 0}

# The user's message, split into words. This is a list of strings.
message = "I need to fly to Delhi tomorrow afternoon"
words = message.lower().split()
# words is now ['i', 'need', 'to', 'fly', 'to', 'delhi', 'tomorrow', 'afternoon']

# A simple scoring rule: words that hint at an intent add points.
# This is a dictionary of dictionaries: word -> intent -> points.
word_hints = {
    "fly": {"book_flight": 3},
    "delhi": {"book_flight": 2},
    "tomorrow": {"book_flight": 1, "check_weather": 1},
    "cancel": {"cancel_trip": 3}
}

# Loop over each word in the message.
for word in words:
    # If this word has hints, update each intent it points to.
    if word in word_hints:
        for intent, points in word_hints[word].items():
            scores[intent] = scores[intent] + points

# Pick the intent with the highest score.
best_intent = max(scores, key=scores.get)

print("Scores:", scores)
print("Best intent:", best_intent)

```


## The same thing, with a framework

```python
In practice, a developer would not write this scoring loop by hand. They would use a library like Rasa or a large language model via an API. With an LLM (e.g., OpenAI's chat completions), the code would be: import openai; response = openai.ChatCompletion.create(model='gpt-4', messages=[{'role':'system','content':'You are a travel assistant. Track intents.'}, {'role':'user','content':'I need to fly to Delhi tomorrow'}]). The framework sends the whole conversation history — the messages list — and the model returns a response. Underneath, the model is doing something far more complex than our scoring loop: it has billions of parameters that assign probabilities to words and intents based on the entire context. But the concept is the same: the model assigns a high likelihood to 'book_flight' and responds accordingly. The difference is that the LLM deduces the intent from the full text, not from a hand-coded word list.
```


## What the code does

We start with a dictionary called scores that holds a number for each possible intent — this is the agent's current belief about what the user wants. The user's message is split into words using .split(), which turns the string into a list of smaller strings — a list is a collection that keeps things in order and can be changed. We then have a second dictionary, word_hints, that maps each word to the intents it supports, with a number saying how strongly. The for loop goes through each word in the list; for each word, if it appears in word_hints, we go through the inner dictionary and add the points to the appropriate intent in scores. After the loop, we call max(scores, key=scores.get) — this finds the intent whose stored value is largest. This is the entire 'intent recognition' step in miniature. In a real system, word_hints would be learned from thousands of conversations, not written by hand, but the mechanism — scoring each intent, picking the highest — is exactly this.


## Common misconceptions

**A chatbot is just a single prompt** — If I send one well-crafted prompt to an LLM, I have a conversational agent.

A single prompt has no memory of prior turns. The moment the user refers back to something said earlier, the model has no way to know what that was unless the history is included in the input. The state is what makes it a conversation. In fact: A conversational agent is defined by its loop: maintain a history, append each new turn, generate a response conditioned on all of it. The prompt matters, but the state management matters more.

**Intent recognition is just keyword matching** — The agent just looks for words like 'book' or 'cancel' to decide what to do.

A user can say 'I want to get to Delhi somehow' without saying 'book' or 'fly'. Real systems learn weights from data and consider the whole sentence, so intent recognition is probabilistic, not rule-based. In fact: Intent recognition scores each candidate intent based on learned evidence from the words and the overall context, and picks the highest-probability one — it can handle paraphrases and indirect requests.

**State is the whole transcript** — Keeping the full chat history is all the memory an agent needs.

A transcript grows unboundedly and may exceed the model's context window. Many systems summarise old turns, keep only recent ones, or extract slot values into a structured state — the transcript is raw material, not the state itself. In fact: State is a compact representation of what matters — the user's goals, decisions, and pending actions. It may be a list of slot-value pairs or a summary, not the raw transcript.

**A conversational agent should never say 'I don't know'** — A good bot must always answer, even if it guesses.

Guessing wrong erodes trust and can cause real harm — a banking bot that guesses a transfer amount is dangerous. Confidence scoring and graceful failure are core design features. In fact: A good agent knows when it is uncertain, asks a clarifying question, or hands off to a human. Graceful failure defines perceived quality more than correct answers.

**It's a chat UI, not a real system** — The hardest part is writing good responses; the engineering around it is trivial.

Production agents need state persistence, context windows, tool integration, error handling, and handoff logic. The response generation is a single step in a much larger loop that decides what to do next. In fact: The architecture is: perceive (parse user message), decide (score intents), act (call a tool or respond), remember (update state), and repeat. The chat interface is just the visible tip.


## In short

A conversational agent is a loop that tracks state across turns. Its three pillars are: (1) intent recognition — scoring what the user wants, (2) state management — remembering what has been established, and (3) graceful failure — knowing when to ask or hand off. Master these and you have the core of every agentic system you'll build next.


## Where this is used

Conversational agents are everywhere in applied AI. In large language models, a chat system like ChatGPT or Gemini keeps a running list of messages — the transcript — and each new turn appends the user's message, then the model generates a response conditioned on the whole list. The state is the conversation history itself; there is no separate 'memory' variable beyond the list. In small models running on phones (e.g., a voice assistant on a smart speaker), the state is compressed — the device cannot hold the entire conversation, so it keeps only the last few turns and a small set of slot values (e.g., 'destination=Delhi', 'date=tomorrow'). In retrieval-augmented generation (RAG), a conversational agent may query a database in the middle of a turn: the user asks 'What is the refund policy?', the agent retrieves relevant documents, and then composes an answer that cites them — the conversational state carries the question, the retrieved passages, and the final answer together. In agentic systems that plan and call tools, a conversational agent is the coordinator: it receives a user request like 'Book me a flight and a hotel', calls a flight API, gets results, calls a hotel API, gets results, and then synthesises a confirmation — all across multiple turns, updating its internal state after each tool call. In everyday applied machine learning, customer-support bots for banks, telecoms and e-commerce use intent recognition (the scoring loop above) and slot filling to extract entities (dates, amounts, account numbers), then hand off to a human when confidence is low. What a developer actually writes: with Rasa, they define stories (conversation paths), intents, and entities in YAML files, then run a pipeline that trains a classifier. With an LLM API, they write a loop: read user input, append to messages, call the model, append the response, repeat. What breaks when done badly: the agent forgets what was said two turns ago, misassigns an intent, or — worst of all — confidentially answers something it does not understand instead of asking for clarification. A well-designed handoff to a human is the difference between a frustrating bot and a useful one. In agentic systems, failure compounds: one wrong intent leads to a wrong tool call, which leads to a wrong result, which the agent then treats as fact. That is why per-step error rates matter far more than in single-shot use.


---

Read this on the web: https://basavapurushottam.com/learn-ai/conversational-agents
