{
  "id": 16336435,
  "title": "Build a Basic AI Agent From Scratch: Session Management II",
  "url": "https://urgent.news/2026/10/24/build-a-basic-ai-agent-from-scratch-session-management-ii",
  "topic": "ai",
  "section": "AI",
  "published": "2026-10-24T14:53:34.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/rogiia/build-a-basic-ai-agent-from-scratch-session-management-ii-489l"
  },
  "original_language": "en",
  "account": "Session compaction is a mechanism designed to help AI agents manage large conversation contexts. Unlike session summaries used in session lists, compaction summaries become the working context for the agent to continue the current session without carrying the entire conversation history. Compaction is performed before each regular model request when the estimated message tokens exceed 80% of the --max-context-tokens limit. The default context window budget is 24000 tokens, so compaction starts after roughly 19200 tokens.\n\nTo ensure a safe split point, the agent chooses the latest user-turn boundary before reaching the token limit. It keeps the system prompt and everything from the latest user message onward, including current turn assistant messages and complete tool batches. Only older turns are summarized. If there are no older turns, no compaction is done.\n\nThe compaction summary itself is generated by an additional LLM call with a specific system prompt. It summarizes conversation data, preserving goals, constraints, decisions, modified files, verification, unresolved tasks, and errors. The summary is a concise factual continuation note and is not included in the system message to prevent prompt-injection issues. Instead, a user data message with an explicit boundary is used to store the continuation message. If the summary is empty or makes the context bigger than before, it is rejected.",
  "summary": "Previous parts of Build a Basic AI Agent From Scratch : Basic Agent Tools Long Task Planning Human in the Loop & Security Security II Security III Memory Session Management You can find and clone this code in this blog series' GitHub repo . In the previous part we gave our agent sessions. It can save a conversation, list and search saved sessions, resume an unfinished one, and recover a tool call…",
  "key_points": [
    "Session compaction helps AI agents manage large conversation contexts.",
    "Compaction occurs when message tokens exceed 80% of the 24000-token limit.",
    "Compaction summary is generated by an additional LLM call with a specific system prompt."
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}