---
title: "Multi-Turn Conversation API: Send History Yourself (3 Steps)"
description: "LLM APIs have no memory: multi-turn chat means resending history every call. See the message pattern, three truncation strategies, and the token cost impact."
url: https://cdnping.org/en/blog/multi-turn-conversation-api-history
language: en
published: 2026-11-26
category: "AI concepts"
tags: ["API","multi-turn conversation","messages","context window","tokens","beginner"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# Multi-Turn Conversation API: Send History Yourself (3 Steps)

LLM APIs are stateless. On each request the model sees only what you send in that request, and it does not automatically remember anything you asked before. So the approach for a multi-turn conversation API is simple: keep the history in your own program, and on every turn send "all previous questions and answers plus the new question."

If a web chat app seems to "remember what I said," that is because the app does this work for you. With the API, the job is yours.

## Why can't the model answer when I ask "who is it?" on the second request?

Say your first request is "I'm learning Python" and you get a reply. Your second request is just "What should I learn next?" The model receives only that one sentence, so it has no idea what "I" am learning. The two requests are independent on the server side and share no session.

API shapes differ a little between providers, and some offer server-side conversation storage; check each provider's official docs. But the most universal approach, supported everywhere, is still for the client to send the history. If you are not yet comfortable with the structure of the messages array, start with [What Is Chat Completions? How to Write the messages Array](/en/blog/what-is-chat-completions-api).

## How do you implement multi-turn conversation? Three steps

The core idea is to maintain a list, append to it each turn, and send the whole thing.

1. Create a list, optionally starting with a system message (to set a role or rules).
2. Each time the user speaks, append a message with `role: "user"` and send the entire list to the API.
3. When the reply comes back, append it to the list as `role: "assistant"` and use the list again on the next turn.

Here is a Python example in the OpenAI-compatible format. `base_url`, `api_key`, and the model name are placeholders; replace them with the values for the service you actually use, and check parameter names against the relevant official docs.

```python
from openai import OpenAI

client = OpenAI(base_url="你的接口地址", api_key="你的 Key")

history = [{"role": "system", "content": "你是一个简洁的编程助手。"}]

def chat(user_text):
    history.append({"role": "user", "content": user_text})
    resp = client.chat.completions.create(
        model="你的模型名",
        messages=history,
    )
    reply = resp.choices[0].message.content
    history.append({"role": "assistant", "content": reply})
    return reply

print(chat("我在学 Python。"))
print(chat("那我下一步学什么？"))  # This time the model can see the previous turn
```

The key point is that `history` is a variable you own. If the program restarts or you switch processes, it is gone; to keep it long term, you need to write it to a file or database yourself. If you don't have a key yet, see [How to Use an LLM API: From Getting a Key to Your First Reply](/en/blog/llm-api-first-request-guide).

## What happens as the history keeps growing?

Resending the full history on every turn means:

- Input tokens grow with each turn. What you send on turn 10 includes all of the first 9 turns of questions and answers.
- Cost rises with it. The API bills by token, and the repeated history counts as input too. Input and output usually have different unit prices (check each provider's pricing page); for why, see [Why Output Tokens Cost More Than Input](/en/blog/why-output-tokens-cost-more).
- The total can hit the model's context window limit. Beyond it, the API usually returns an error outright; the exact error message is in the official docs.

If tokens themselves are unfamiliar, read [What Is a Token in an LLM?](/en/blog/what-is-token-in-llm) first. To work out this cost, use [How to Estimate LLM API Cost: A Formula You Can Reuse](/en/blog/llm-api-cost-estimation-method) and factor in the "average history length per turn."

A rough intuition (an illustration, not measured data): if each question-and-answer pair averages 500 tokens, turn 1 sends about 500, turn 10 sends about 5,000, and ten turns send about 27,500 input tokens in total, several times the 5,000 you would send if each turn were independent at 500. The more turns, the bigger the gap.

## History too long? Three common strategies

| Strategy | How it works | Pros | Cost |
| --- | --- | --- | --- |
| Sliding window | Keep only the most recent N messages; always keep the system message | Simplest to implement, predictable cost | Early content is simply lost |
| Truncate by token | Estimate total tokens; when over budget, delete from the oldest turn | More precise than counting messages | Needs a token-counting tool; estimates can be off |
| Summarize | Have the model summarize older conversation into a paragraph and replace the original with the summary | Keeps the gist in a small footprint | One extra call; the summary may lose details |

In practice they are often combined: keep the system message fixed, compress early content into a summary, and keep the most recent few turns verbatim.

A few easy pitfalls:

- When truncating, delete in question-and-answer pairs so you don't leave an orphaned assistant message.
- If the conversation includes tool calls, keep each tool call together with its result, or the API may return a format error; check the official docs for the rules.
- Summaries are generated by the model. Keep numbers, code, and details the user explicitly asked for verbatim, or store them in a separate "key facts" field.

## Pre-ship checklist

- Does every request include the full messages list, not just the latest sentence?
- Is the model's reply appended back to history?
- Have you set a length cap on the history so long conversations don't quietly get expensive?
- Is the system message present in every request?
- In a multi-user setup, is each user's history stored separately rather than mixed in one list?

## How a beginner can get started

Run the few dozen lines of code above locally and get a two-turn conversation working, then add a check such as "trim if there are more than N messages." Once you have done that, you have the basic skeleton of a chat application.

If you want to use tools like Claude Code or Codex and prefer to pay monthly in RMB, you can look at AGI Plan. AGI Plan is a commercial partner of cdnping. It is a monthly-subscription, RMB-billed model aggregation gateway: one subscription lets you switch between multiple capability groups, and tools such as Claude Code and Codex only need a single endpoint URL changed. Some models are marked as available only outside mainland China; check the [AGI Plan models page](https://agiplan.dev/models) for specifics, and note that the site states plainly that it is not an officially authorized channel. For setup steps, see the [AGI Plan getting-started guide](https://agiplan.dev/startup).

Once your API calls work, if responses feel slow, you can use cdnping's [online speed test](/en) to see first-connection and round-trip times for each provider's API; for background, see [AI API Latency and Access from Mainland China](/en/learn/ai-api-latency-china). Next step: set a history length cap locally first, then estimate your monthly cost once with the formula above.
