跳到正文
DEV Community· Altrouter·· 52 分鐘前AI 評分66

每次回覆後更新聊天摘要,令 4 輪對話成本增加 84%

Updating a chat summary after every reply made my 4-turn chats 84% more expensive

AI 導讀

作者比較三種聊天歷史處理策略,在其 gpt-5.6 價格模型中,每次回覆後更新摘要令 4 輪對話成本增加 84%。作者建議待未摘要對話累積至 10 輪再批次處理,該方案在第 12 輪起比傳送完整歷史便宜;文章只比較成本,沒有測量回答質素。

正文

You've built a chatbot, and you've learned that chat APIs have no memory: every turn you send the system prompt and the whole conversation again, so long chats get expensive fast. The usual fix is a running summary. Instead of the full history, you send a short summary of what was said so far.

The obvious way to build it is to update the summary after every reply. It's simple and the summary is always fresh. On short chats it's also the most expensive option, because the summary is text the model writes, and written text is billed at a much higher price.

A few words first

A token is a chunk of text the model reads or writes; APIs bill per million of them. Input tokens are what you send, output tokens are what the model writes back, and output usually costs several times more. A list price is the vendor's official price per million tokens, before any discount. A turn is one user message plus one assistant reply. A summarizer call is a separate API request whose only job is to rewrite the conversation into a shorter text.

The setup

I modeled a support chat with round numbers: a 400-token system prompt, 60-token user messages, 250-token replies and a 250-token summary (about 200 words). The summarizer instruction, counted with tiktoken (o200k_base), is 83 tokens. Prices are list prices from the catalog snapshot of the LLM gateway I work on: gpt-5.6 at $2 per million input tokens and $12 per million output.

I compared three strategies for the same conversation:

  1. Full history. Send everything, every turn.
  2. Summary after every reply. After each answer, ask the model to merge the last turn into the summary. The next turn sends the system prompt, the summary, the last turn word for word and the new message.
  3. Summary when ten turns pile up. Send full history until there are ten unsummarized turns, then fold all but the last two into the summary in one call.

What it costs

Total cost of one conversation on gpt-5.6, chat and summarizer on the same model:

Turns Full history Summary after every reply Summary when 10 turns pile up
4 $0.0194 $0.0357 (+84%) $0.0194 (no summary yet)
10 $0.0671 $0.0916 (+37%) $0.0671 (no summary yet)
20 $0.1962 $0.1849 (−6%) $0.1584 (−19%)
50 $0.9555 $0.4647 (−51%) $0.4229 (−56%)

The per-reply summary only becomes cheaper than doing nothing at turn 19. Most support chats are over long before that.

The reason is the output price. Each per-reply summarizer call reads 643 tokens and writes 250. Those 250 output tokens cost as much as 1,500 input tokens, so 70% of the call's price is the summary text itself. The whole call costs $0.0043, more than the very first chat turn ($0.0039). On a short chat you pay that every turn to avoid resending a history that was still small.

The batched version has the same per-call problem, but it pays it rarely and only when it buys something. Its first summarizer call costs $0.0081: it reads eight turns (2,480 tokens) and replaces them with 250. From then on every turn sends about 2,230 fewer input tokens, which saves $0.0045 per turn. The call has paid for itself after two more turns: the conversation is 5% more expensive at turn 11, 1% cheaper at turn 12, and 19% cheaper at turn 20. A chat that ends before turn 11 never triggers it and costs exactly what full history costs.

Moving the summary to a cheaper model shifts both curves. With gpt-5.6-mini ($0.20 / $1.20) writing summaries for a gpt-5.6 chat, the per-reply version is still 7% more expensive at four turns but cheaper from turn 6, and the batched one is cheaper from the first summary. The batched version on claude-sonnet-5.5 with claude-haiku-5.5 as the summarizer is also cheaper from its first summary.

The fix: summarize when it's needed, not on a schedule

Check the size before each turn and summarize only when the history has grown:

def build_messages(system, summary, turns, new_msg, summarize, max_turns=10, keep=2):
    """turns: list of (user, assistant) pairs not yet in the summary."""
    if len(turns) >= max_turns:
        old, turns[:] = turns[:-keep], turns[-keep:]
        summary = summarize(summary, old)          # one call, only here
    msgs = [{"role": "system", "content": system}]
    if summary:
        msgs.append({"role": "system", "content": "Conversation so far: " + summary})
    for u, a in turns:
        msgs += [{"role": "user", "content": u}, {"role": "assistant", "content": a}]
    msgs.append({"role": "user", "content": new_msg})
    return msgs, summary

fake = lambda s, old: f"{len(old)} earlier turns about a refund"
turns = [(f"q{i}", f"a{i}") for i in range(10)]
msgs, s = build_messages("You are support.", "", turns, "q10", fake)
print(len(msgs), s)

Running it prints 7 8 earlier turns about a refund: the system prompt, the summary, two recent turns (four messages) and the new message. Ten turns is a starting point. A count of tokens works better than a count of turns if your messages vary a lot in length, and the usage field of each response tells you the real input size.

What this doesn't solve

A summary loses detail. The model may drop an order number or a setting the user mentioned on turn 3, and with the batched version that loss happens in one jump, not gradually. I didn't measure answer quality, only price. If your bot must quote exact details from early in the chat, test summaries on real conversations first, or keep key facts in a separate structured field instead.

Prompt caching changes the math. Many vendors bill a repeated prefix at a discount. Full history is a prefix that only grows, so it caches well, while every new summary changes the prefix and breaks the cache. The catalog I used has no cache prices, so none of this is in the table.

Finally, all token sizes here are assumptions, and the break-even points move with them. Longer replies make summarizing pay off sooner; a summary close to the size of the turns it replaces never pays off. Plug your own numbers from usage into the same comparison before you pick a threshold.

來源:DEV Community · dev.to