Memoria de resumen y truncamiento consciente de los tokens
Usará ConversationSummaryMemory para resumir automáticamente los turnos antiguos, manteniendo la conversación condensada y conservando los datos clave mencionados anteriormente por la persona usuaria.
Memoria de resumen y truncamiento consciente de los tokens es una lección gratuita de AI Engineering Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de AI Engineering Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de AI Engineering Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
The Token Cost of Full History
Conversation Buffer Memory keeps every message ever exchanged, which quickly consumes your context window. A 100-turn conversation might use 20,000 tokens just for history, leaving little room for the actual response. Summary Memory solves this by replacing old turns with a compressed summary.
How Summary Memory Works
When total tokens exceed a threshold, ConversationSummaryMemory feeds the oldest conversation turns to an LLM and asks it to summarize the key points. The full turns are discarded and replaced by this compact summary. Future turns accumulate on top of the summary.
- Old turns: replaced by summary
- Recent turns: kept verbatim
- Net result: meaningful compression with minimal information loss
LangChain ConversationSummaryMemory
LangChain provides ConversationSummaryMemory that automatically summarizes whenever the buffer grows too large. You pass an LLM to the memory object so it can call the model to generate summaries on demand.
from langchain.memory import ConversationSummaryMemory
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model='gpt-4o-mini')
memory = ConversationSummaryMemory(
llm=llm,
return_messages=True
)
# Add messages manually
memory.save_context(
{'input': 'My name is Alice and I am building a RAG system.'},
{'output': 'Great, I can help you build a RAG system, Alice.'}
)
print(memory.load_memory_variables({}))Summary Buffer Memory: Best of Both
ConversationSummaryBufferMemory is a hybrid approach: it keeps the most recent turns verbatim for accuracy and summarizes only the older turns that exceed a max_token_limit. This gives you exact recall of recent context plus compressed recall of older context.
from langchain.memory import ConversationSummaryBufferMemory
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model='gpt-4o-mini')
memory = ConversationSummaryBufferMemory(
llm=llm,
max_token_limit=500, # Summarize when older turns exceed 500 tokens
return_messages=True
)
# As the conversation grows, old turns get summarized automatically
print('Moving summary:', memory.moving_summary_buffer)Counting Tokens Before Truncation
To make smart truncation decisions, you need to know how many tokens your messages consume. The tiktoken library lets you count tokens for any OpenAI model. This lets you implement token-aware truncation that stays strictly under a budget.
import tiktoken
def count_tokens(messages: list, model: str = 'gpt-4o') -> int:
encoding = tiktoken.encoding_for_model(model)
total = 0
for msg in messages:
# Each message has overhead tokens for role framing
total += 4
total += len(encoding.encode(msg.get('content', '')))
total += 2 # Reply primer
return total
messages = [
{'role': 'user', 'content': 'Explain RAG to me.'},
{'role': 'assistant', 'content': 'RAG stands for Retrieval-Augmented Generation...'}
]
print('Token count:', count_tokens(messages))Manual Token-Aware Truncation
Sometimes you want full control over truncation without LangChain's memory classes. A simple approach: keep all messages, then remove the oldest non-system messages one at a time until the total token count fits within your budget.
def truncate_to_budget(messages: list, budget: int, model: str = 'gpt-4o') -> list:
'''Remove oldest non-system messages until under budget.'''
import tiktoken
encoding = tiktoken.encoding_for_model(model)
def token_count(msgs):
total = 2
for m in msgs:
total += 4 + len(encoding.encode(m.get('content', '')))
return total
result = list(messages)
while token_count(result) > budget and len(result) > 1:
# Never remove the system prompt at index 0
if result[0]['role'] == 'system':
del result[1]
else:
del result[0]
return resultGenerating the Summary Prompt
When you build your own summary memory, you craft a prompt asking the LLM to distill the key facts. The prompt should instruct the model to capture user preferences, named entities, and unresolved questions — the facts most likely to matter in future turns.
from openai import OpenAI
client = OpenAI()
def summarize_history(old_summary: str, new_turns: list) -> str:
turns_text = '\n'.join(
f'{m["role"].capitalize()}: {m["content"]}' for m in new_turns
)
prompt = f'''Current summary:\n{old_summary}\n\nNew conversation turns:\n{turns_text}\n\nWrite an updated summary that captures key facts, user preferences, and open questions.'''
resp = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': prompt}]
)
return resp.choices[0].message.contentIntegrating Summary Into the System Prompt
Once you have a summary, inject it into the system prompt so the LLM always has context about the conversation history. Prepend the summary as a brief context block before the main persona instructions.
def build_messages_with_summary(summary: str, recent_turns: list, user_input: str) -> list:
system_content = (
'You are a helpful AI assistant.'
+ ('\n\n[Conversation summary]\n' + summary if summary else '')
)
messages = [{'role': 'system', 'content': system_content}]
messages.extend(recent_turns)
messages.append({'role': 'user', 'content': user_input})
return messagesTriggering Summarization Automatically
A common pattern is to trigger summarization when the conversation exceeds a token threshold — for example, when the running history crosses 2,000 tokens. At that point, you summarize all but the last two turns and replace them with the summary.
class SummaryBufferChat:
def __init__(self, max_tokens=2000):
self.summary = ''
self.recent_turns = []
self.max_tokens = max_tokens
def chat(self, user_message: str) -> str:
from openai import OpenAI
client = OpenAI()
messages = build_messages_with_summary(self.summary, self.recent_turns, user_message)
resp = client.chat.completions.create(model='gpt-4o', messages=messages)
reply = resp.choices[0].message.content
self.recent_turns.append({'role': 'user', 'content': user_message})
self.recent_turns.append({'role': 'assistant', 'content': reply})
if count_tokens(self.recent_turns) > self.max_tokens:
to_summarize = self.recent_turns[:-2]
self.recent_turns = self.recent_turns[-2:]
self.summary = summarize_history(self.summary, to_summarize)
return replyPreserving Named Entities in Summaries
Summary quality depends heavily on the summarization prompt. If users mention their name, location, preferences, or deadlines, your prompt must explicitly instruct the model to include those facts. Generic summaries often drop proper nouns that matter most for personalization.
- Include: names, dates, technical choices made, open questions
- Exclude: filler exchanges, acknowledgements, repeated pleasantries
Trade-offs: Summary vs. Window Memory
Choosing between summary and window memory depends on your use case. Window memory preserves exact wording, which matters for precise recall but wastes tokens on irrelevant context. Summary memory compresses aggressively but introduces an extra LLM call and may lose edge-case details. Most production chatbots use a hybrid: recent exact turns plus an older rolling summary.
Quick Check
Test your understanding of summary memory and token-aware truncation concepts.
Lesson Recap
In this lesson you learned: summary memory compresses old turns via an LLM call, ConversationSummaryBufferMemory hybrids exact recent turns with summarized older ones, and tiktoken enables token-aware truncation to keep conversations within budget. Next up we explore persisting chat history in Redis and PostgreSQL for durable, scalable storage.
Preguntas frecuentes
¿La lección «Memoria de resumen y truncamiento consciente de los tokens» es gratis?
Sí — el texto completo de «Memoria de resumen y truncamiento consciente de los tokens» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de AI Engineering Academy, actualiza a CoddyKit PRO. El curso de AI Engineering Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Memoria de resumen y truncamiento consciente de los tokens»?
Usará ConversationSummaryMemory para resumir automáticamente los turnos antiguos, manteniendo la conversación condensada y conservando los datos clave mencionados anteriormente por la persona usuaria. Practicas AI Engineering Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar AI Engineering Academy?
No se requiere experiencia previa. AI Engineering Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Memoria de resumen y truncamiento consciente de los tokens»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de AI Engineering Academy?
Sí. Cada lección de AI Engineering Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Por qué los LLM sin estado necesitan memoria externa
- Memoria de buffer y de ventana
- Memoria de resumen y truncamiento consciente de los tokens
- Persistencia del historial de chat en Redis y PostgreSQL