History compaction: chat.compact()
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia leggendo Chat.token_count() e set_turns() in chatlas/_chat.py, quindi esamina _chat_impl/_chat_impl_async e i tipi di contenuto e turni in chatlas/_content.py e chatlas/_turn.py. Risolvi prima dell’implementazione le questioni di progettazione aperte relative ai confini della summarization, ai contenuti non testuali, alla contabilizzazione e all’attivazione automatica; il lavoro è completo quando sono disponibili un’API compact() definita e un comportamento verificato per le chat sincrone e asincrone.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Motivation
Long-running chatlas sessions (agents, chatbots, notebooks kept open all day) eventually blow the context window, and chatlas currently has no built-in remedy beyond manually calling chat.set_turns() to prune history yourself. LangChain 1.0 ships SummarizationMiddleware, which auto-summarizes older messages once a token threshold is hit, and this pattern is now standard in agent products (Claude Code included). This is arguably the concrete design proposal for chatlas#8 ("Make it easier to impose limits on conversation size") — this issue frames a specific mechanism to close (part of) that issue.
Proposed approach
A plain method, not hidden middleware — explicit and inspectable, in keeping with chatlas's existing style (chat.set_turns(), chat.get_turns() in chatlas/_chat.py):
from chatlas import ChatAnthropic
chat = ChatAnthropic()
# ... long conversation ...
# Manual, opt-in trigger (default):
if chat.token_count(include="complete") > 100_000:
chat.compact() # and chat.compact_async()
# Optionally, summarize with a cheaper/faster model than the main conversation:
chat.compact(model=ChatAnthropic(model="claude-haiku-4-5"))
# Opt-in automatic triggering, off by default:
chat.compact(auto=True, threshold=100_000) # checked at the start of each .chat() call
Implementation sketch:
- Leverage
Chat.token_count()/token_count_async()(chatlas/_chat.py:671), specifically the recently-addedinclude="complete"mode, which already estimates system prompt +.get_turns()history — exactly the heuristic needed to decide whether to compact. compact()selects a prefix ofself._turnsto summarize (e.g. all but the last N turns, or all but the last complete user/assistant pair), submits them to the provider (or a caller-suppliedChatfor a cheaper model) with a summarization prompt, and replaces that prefix with a single synthetic turn pair (aUserTurn/SystemTurnnote plus anAssistantTurncontaining the summary asContentText) viaChat.set_turns()(chatlas/_chat.py:357), which already supports exactly this "trim and replace" use case per its own docstring ("Clearing (or trimming) the chat history").- Non-text content during summarization:
ContentToolRequest/ContentToolResultpairs,ContentImageInline/ContentPDF, etc. (chatlas/_content.py) inside the summarized range should be described in the summary text (e.g. "user uploaded a PDF; assistant calledsearch_docs(query=...)and got N results") rather than silently dropped, since dropping tool results that established facts the model relied on can cause it to contradict itself later.Turn.__str__(chatlas/_turn.py:115) already gives a reasonable string form of each content type to seed this. AssistantTurn.cost/.tokens(chatlas/_turn.py:340,343) for the summarized turns are lost fromget_tokens()/get_cost()accounting once replaced — this should be documented as expected (cost already spent isn't un-spent) and possibly preserved as metadata on the synthetic turn rather than silently discarded.
Alternatives / prior art
- Closes/addresses part of chatlas#8 directly.
- No direct ellmer equivalent found (searched
tidyverse/ellmerfor "compact", "summar" — no hits), so this would be new ground for the ellmer/chatlas pairing; worth raising upstream once chatlas's design is validated, per the project's alignment goal. - Alternative considered and rejected for v1: automatic middleware-style compaction with no explicit call, à la LangChain's
SummarizationMiddleware. Given YAGNI and that chatlas favors explicit methods over hidden hooks (see howset_model_params(),set_turns()are all explicit calls), default to manual/explicit triggering, withauto=Trueas an opt-in convenience layered on top of the same method.
Open questions
- Where should the threshold check live if
auto=True— inside_chat_impl/_chat_impl_async(chatlas/_chat.py:2647,2734) before submitting turns, or as a wrapper the caller opts into explicitly? - Should
compact()support summarizing tool results into a persistedContentToolResult-shaped stub token (to keep e.g.expand_tool_resultinchatlas/_content_expand.pyand dangling-request logic inchatlas/_turn.pyhappy) rather than plain text? - Is a single summary turn enough, or should compaction be tiered (e.g. keep a rolling summary + last K turns, re-summarizing as needed)?
- Should the system prompt ever be folded into compaction, or always preserved verbatim (current
set_turns()behavior preserves it automatically)?
Drafted from a competitive review of llm / Pydantic AI / LangChain / LiteLLM (July 2026); filed via Claude Code on behalf of @cpsievert.
- Lingua principale
- Python
- Stelle
- 179
- Fork
- 32
- Merge medio
- 1g 3h
- PR unite (30g)
- 10
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di posit-dev/chatlas
-
A streaming text delta that repeats everything streamed so far is missing from the recorded turnForse già presa @cpsievert l’ha presa 81 giorni fa. Apertaai-triage:done Priority: Medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
ai-triage:needs-review documentation
Difficoltà 2/5 1-2 giorni Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 1-2 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
ai-triage:done Priority: Low
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di posit-dev/chatlas
Issue simili
-
bot-triaged module: decompositions module: edge cases module: int overflow module: primTorch oncall: pt2 triage review
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
[TV SHOW]: Dynasty (1981)Apertacountry:US rating:6 request-theme type:TV SHOW
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
LizardByte/ThemerrDB#10501 · 1 commento ·
-
triage/confirmed
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
agentscope-ai/agentscope#4563 ·
I maintainer di solito rispondono entro 2 giorni
-
comp/desktop desktop/chat P3 type/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
NousResearch/hermes-agent#164451 ·
I maintainer di solito rispondono entro 1 giorno
-
external integration integration-submission
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
langchain-ai/docs#8426 ·
I maintainer di solito rispondono entro 1 giorno