[{"data":1,"prerenderedAt":678},["ShallowReactive",2],{"navigation_docs":3,"-context-engineering-sliding-window":208,"-context-engineering-sliding-window-surround":673},[4,33,60,82,109,151],{"title":5,"path":6,"stem":7,"children":8,"icon":32},"OpenSSL","/openssl","1.openssl/1.index",[9,12,17,22,27],{"title":10,"path":6,"stem":7,"icon":11},"Overview","i-lucide-info",{"title":13,"path":14,"stem":15,"icon":16},"Certificates","/openssl/certificates","1.openssl/2.certificates","i-lucide-file-badge",{"title":18,"path":19,"stem":20,"icon":21},"Keys","/openssl/keys","1.openssl/3.keys","i-lucide-key",{"title":23,"path":24,"stem":25,"icon":26},"Encryption","/openssl/encryption","1.openssl/4.encryption","i-lucide-shield",{"title":28,"path":29,"stem":30,"icon":31},"Verification","/openssl/verification","1.openssl/5.verification","i-lucide-check-circle","i-lucide-lock",{"title":34,"path":35,"stem":36,"children":37,"icon":59},"Voice AI","/voice-ai","2.voice-ai/1.index",[38,39,44,49,54],{"title":10,"path":35,"stem":36,"icon":11},{"title":40,"path":41,"stem":42,"icon":43},"Twilio Media Streams","/voice-ai/twilio","2.voice-ai/2.twilio","i-lucide-radio",{"title":45,"path":46,"stem":47,"icon":48},"WebSocket & Streaming","/voice-ai/websocket","2.voice-ai/3.websocket","i-lucide-cable",{"title":50,"path":51,"stem":52,"icon":53},"LLM Integration","/voice-ai/llm","2.voice-ai/4.llm","i-lucide-brain",{"title":55,"path":56,"stem":57,"icon":58},"Speech Services","/voice-ai/speech","2.voice-ai/5.speech","i-lucide-audio-lines","i-lucide-phone",{"title":61,"path":62,"stem":63,"children":64,"icon":81},"Agents","/agents","3.agents/1.index",[65,66,71,76],{"title":10,"path":62,"stem":63,"icon":11},{"title":67,"path":68,"stem":69,"icon":70},"RAG","/agents/rag","3.agents/2.rag","i-lucide-database",{"title":72,"path":73,"stem":74,"icon":75},"ReAct Agents","/agents/react-agents","3.agents/3.react-agents","i-lucide-activity",{"title":77,"path":78,"stem":79,"icon":80},"OpenAI Agent","/agents/openai-agent","3.agents/4.openai-agent","i-lucide-bot","i-lucide-users",{"title":83,"path":84,"stem":85,"children":86,"icon":88},"Context Engineering","/context-engineering","4.context-engineering/1.index",[87,89,94,99,104],{"title":83,"path":84,"stem":85,"icon":88},"i-lucide-brain-circuit",{"title":90,"path":91,"stem":92,"icon":93},"Managing The Context Window","/context-engineering/managing-context-window","4.context-engineering/2.managing-context-window","i-lucide-settings",{"title":95,"path":96,"stem":97,"icon":98},"Sliding Window Strategy","/context-engineering/sliding-window","4.context-engineering/3.sliding-window","i-lucide-arrow-left-right",{"title":100,"path":101,"stem":102,"icon":103},"Token-based Management","/context-engineering/token-based","4.context-engineering/4.token-based","i-lucide-hash",{"title":105,"path":106,"stem":107,"icon":108},"Summarization Techniques","/context-engineering/summarization","4.context-engineering/5.summarization","i-lucide-file-text",{"title":110,"path":111,"stem":112,"children":113,"icon":150},"AI SDK 6","/ai-sdk","5.ai-sdk/1.index",[114,115,120,125,130,135,140,145],{"title":10,"path":111,"stem":112,"icon":11},{"title":116,"path":117,"stem":118,"icon":119},"Installation & Setup","/ai-sdk/installation","5.ai-sdk/2.installation","i-lucide-package",{"title":121,"path":122,"stem":123,"icon":124},"Model Method","/ai-sdk/model","5.ai-sdk/3.model","i-lucide-box",{"title":126,"path":127,"stem":128,"icon":129},"generateText","/ai-sdk/generate-text","5.ai-sdk/4.generate-text","i-lucide-message-square",{"title":131,"path":132,"stem":133,"icon":134},"streamText","/ai-sdk/stream-text","5.ai-sdk/5.stream-text","i-lucide-zap",{"title":136,"path":137,"stem":138,"icon":139},"Structured Output","/ai-sdk/structured-output","5.ai-sdk/6.structured-output","i-lucide-layers",{"title":141,"path":142,"stem":143,"icon":144},"Text Embeddings","/ai-sdk/embeddings","5.ai-sdk/7.embeddings","i-lucide-git-branch",{"title":146,"path":147,"stem":148,"icon":149},"Agent Loop – ToolLoopAgent","/ai-sdk/agent-loop","5.ai-sdk/8.agent-loop","i-lucide-refresh-cw","i-lucide-cpu",{"title":152,"icon":153,"path":154,"stem":155,"children":156,"page":207},"Syntax Reference","i-lucide-book-open","/syntax-reference","999.syntax-reference",[157,162,167,172,177,182,187,192,197,202],{"title":158,"path":159,"stem":160,"icon":161},"Markdown Syntax","/syntax-reference/markdown-syntax","999.syntax-reference/1.markdown-syntax","i-lucide-heading-1",{"title":163,"path":164,"stem":165,"icon":166},"Code Blocks","/syntax-reference/code-blocks","999.syntax-reference/2.code-blocks","i-lucide-code-xml",{"title":168,"path":169,"stem":170,"icon":171},"Introduction","/syntax-reference/introduction","999.syntax-reference/2.introduction","i-lucide-house",{"title":173,"path":174,"stem":175,"icon":176},"Components","/syntax-reference/components","999.syntax-reference/3.components","i-lucide-component",{"title":178,"path":179,"stem":180,"icon":181},"Installation","/syntax-reference/installation","999.syntax-reference/3.installation","i-lucide-download",{"title":183,"path":184,"stem":185,"icon":186},"Images and Embeds","/syntax-reference/images-embeds","999.syntax-reference/4.images-embeds","i-lucide-image",{"title":188,"path":189,"stem":190,"icon":191},"Project Structure","/syntax-reference/project-structure","999.syntax-reference/4.project-structure","i-lucide-folder-tree",{"title":193,"path":194,"stem":195,"icon":196},"Studio module","/syntax-reference/studio","999.syntax-reference/5.studio","i-lucide-mouse-pointer-2",{"title":198,"path":199,"stem":200,"icon":201},"Migration","/syntax-reference/migration","999.syntax-reference/6.migration","i-lucide-replace",{"title":203,"path":204,"stem":205,"icon":206},"Troubleshooting","/syntax-reference/troubleshooting","999.syntax-reference/7.troubleshooting","i-lucide-wrench",false,{"id":209,"title":95,"body":210,"description":666,"extension":667,"links":668,"meta":669,"navigation":670,"path":96,"seo":671,"stem":97,"__hash__":672},"docs/4.context-engineering/3.sliding-window.md",{"type":211,"value":212,"toc":632},"minimark",[213,217,222,226,231,242,246,338,342,348,352,360,366,373,376,383,389,396,399,405,409,415,418,431,437,440,451,457,460,471,475,481,534,540,543,587,593,596,607,611,614,625,628],[214,215,95],"h1",{"id":216},"sliding-window-strategy",[218,219,221],"h2",{"id":220},"how-sliding-context-window-works","How Sliding Context Window Works",[223,224,225],"p",{},"A sliding context window maintains only the most recent messages within the context limit. When new messages arrive, older messages are \"pushed out\" of the window.",[227,228,230],"h3",{"id":229},"basic-mechanism","Basic Mechanism",[232,233,238],"pre",{"className":234,"code":236,"language":237},[235],"language-text","Initial State (Window Size = 3):\n┌─────────────────────────┐\n│ [A] [B] [C]            │ ← Context Window\n└─────────────────────────┘\n\nAfter Adding Message D:\n┌─────────────────────────┐\n│ [B] [C] [D]            │ ← A is dropped\n└─────────────────────────┘\n\nAfter Adding Message E:\n┌─────────────────────────┐\n│ [C] [D] [E]            │ ← B is dropped\n└─────────────────────────┘\n","text",[239,240,236],"code",{"__ignoreMap":241},"",[227,243,245],{"id":244},"implementation-example","Implementation Example",[232,247,251],{"className":248,"code":249,"language":250,"meta":241,"style":241},"language-python shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","class SlidingWindow:\n    def __init__(self, window_size=3):\n        self.window_size = window_size\n        self.messages = []\n    \n    def add_message(self, message):\n        self.messages.append(message)\n        \n        # Keep only the most recent messages\n        if len(self.messages) > self.window_size:\n            self.messages = self.messages[-self.window_size:]\n    \n    def get_context(self):\n        return self.messages\n","python",[239,252,253,261,267,273,279,285,291,297,303,309,315,321,326,332],{"__ignoreMap":241},[254,255,258],"span",{"class":256,"line":257},"line",1,[254,259,260],{},"class SlidingWindow:\n",[254,262,264],{"class":256,"line":263},2,[254,265,266],{},"    def __init__(self, window_size=3):\n",[254,268,270],{"class":256,"line":269},3,[254,271,272],{},"        self.window_size = window_size\n",[254,274,276],{"class":256,"line":275},4,[254,277,278],{},"        self.messages = []\n",[254,280,282],{"class":256,"line":281},5,[254,283,284],{},"    \n",[254,286,288],{"class":256,"line":287},6,[254,289,290],{},"    def add_message(self, message):\n",[254,292,294],{"class":256,"line":293},7,[254,295,296],{},"        self.messages.append(message)\n",[254,298,300],{"class":256,"line":299},8,[254,301,302],{},"        \n",[254,304,306],{"class":256,"line":305},9,[254,307,308],{},"        # Keep only the most recent messages\n",[254,310,312],{"class":256,"line":311},10,[254,313,314],{},"        if len(self.messages) > self.window_size:\n",[254,316,318],{"class":256,"line":317},11,[254,319,320],{},"            self.messages = self.messages[-self.window_size:]\n",[254,322,324],{"class":256,"line":323},12,[254,325,284],{},[254,327,329],{"class":256,"line":328},13,[254,330,331],{},"    def get_context(self):\n",[254,333,335],{"class":256,"line":334},14,[254,336,337],{},"        return self.messages\n",[227,339,341],{"id":340},"real-world-scenario","Real-world Scenario",[232,343,346],{"className":344,"code":345,"language":237},[235],"Conversation:\nUser: \"I'm working on a Python web app\"\nAssistant: \"Great! What framework are you using?\"\nUser: \"Django with PostgreSQL\"\nAssistant: \"Excellent choice! What's your app about?\"\nUser: \"It's an inventory management system\"\nAssistant: \"Nice! What specific features do you need?\"\n\nSliding Window (last 3 messages):\n┌─────────────────────────────────────────────────┐\n│ \"What specific features do you need?\"           │\n│ \"It's an inventory management system\"           │\n│ \"Nice! What specific features do you need?\"     │\n└─────────────────────────────────────────────────┘\n\nProblem: Model no longer knows we're using Django and PostgreSQL!\n",[239,347,345],{"__ignoreMap":241},[218,349,351],{"id":350},"problems-with-sliding-window","Problems with Sliding Window",[227,353,355,356],{"id":354},"_1-loss-of-critical-context","1. ",[357,358,359],"strong",{},"Loss of Critical Context",[232,361,364],{"className":362,"code":363,"language":237},[235],"Original Context:\nUser: \"My name is John, I'm a frontend developer\"\nAssistant: \"Nice to meet you, John!\"\nUser: \"Can you help me with React?\"\nAssistant: \"Sure, what React issue are you facing?\"\nUser: \"I'm having trouble with hooks\"\nAssistant: \"What specific hook problem?\"\n\nAfter Sliding:\n┌─────────────────────────────────────────┐\n│ \"I'm having trouble with hooks\"         │\n│ \"What specific hook problem?\"           │\n│ \"Sure, what React issue are you facing?\" │\n└─────────────────────────────────────────┘\n\nResult: Model doesn't know the user's name or that they're a developer!\n",[239,365,363],{"__ignoreMap":241},[227,367,369,370],{"id":368},"_2-topic-drift","2. ",[357,371,372],{},"Topic Drift",[223,374,375],{},"The model may lose track of the original conversation topic and provide irrelevant responses.",[227,377,379,380],{"id":378},"_3-repetitive-questions","3. ",[357,381,382],{},"Repetitive Questions",[232,384,387],{"className":385,"code":386,"language":237},[235],"User: \"What's my name?\"\nAssistant: \"I don't know your name.\"\nUser: \"I told you earlier, I'm John\"\nAssistant: \"Nice to meet you, John!\"\nUser: \"Now what's my name?\"\nAssistant: \"I don't know your name.\" ← Context window slid again!\n",[239,388,386],{"__ignoreMap":241},[227,390,392,393],{"id":391},"_4-loss-of-instructions","4. ",[357,394,395],{},"Loss of Instructions",[223,397,398],{},"System prompts and important instructions can be lost:",[232,400,403],{"className":401,"code":402,"language":237},[235],"Initial:\nSystem: \"Always respond in JSON format\"\nUser: \"Convert this to JSON\"\nAssistant: {\"status\": \"ok\"}\n\nLater (after sliding):\nUser: \"Convert this to JSON\"\nAssistant: \"Sure, here's the conversion:\" ← Forgot JSON format!\n",[239,404,402],{"__ignoreMap":241},[218,406,408],{"id":407},"when-sliding-window-works-well","When Sliding Window Works Well",[227,410,355,412],{"id":411},"_1-short-conversations",[357,413,414],{},"Short Conversations",[223,416,417],{},"For brief interactions where context loss isn't critical:",[419,420,421,425,428],"ul",{},[422,423,424],"li",{},"Simple Q&A",[422,426,427],{},"Code generation",[422,429,430],{},"Translation tasks",[227,432,369,434],{"id":433},"_2-stateless-operations",[357,435,436],{},"Stateless Operations",[223,438,439],{},"When each request is largely independent:",[419,441,442,445,448],{},[422,443,444],{},"API calls",[422,446,447],{},"Data processing",[422,449,450],{},"Content generation",[227,452,379,454],{"id":453},"_3-real-time-chat",[357,455,456],{},"Real-time Chat",[223,458,459],{},"For live conversations where only recent messages matter:",[419,461,462,465,468],{},[422,463,464],{},"Customer support",[422,466,467],{},"Live assistance",[422,469,470],{},"Quick troubleshooting",[218,472,474],{"id":473},"advanced-sliding-window-techniques","Advanced Sliding Window Techniques",[227,476,355,478],{"id":477},"_1-priority-sliding",[357,479,480],{},"Priority Sliding",[232,482,484],{"className":248,"code":483,"language":250,"meta":241,"style":241},"def priority_sliding(messages, window_size):\n    # Always keep system messages\n    system_msgs = [m for m in messages if m['role'] == 'system']\n    other_msgs = [m for m in messages if m['role'] != 'system']\n    \n    # Keep recent messages within window size\n    available_space = window_size - len(system_msgs)\n    recent_msgs = other_msgs[-available_space:]\n    \n    return system_msgs + recent_msgs\n",[239,485,486,491,496,501,506,510,515,520,525,529],{"__ignoreMap":241},[254,487,488],{"class":256,"line":257},[254,489,490],{},"def priority_sliding(messages, window_size):\n",[254,492,493],{"class":256,"line":263},[254,494,495],{},"    # Always keep system messages\n",[254,497,498],{"class":256,"line":269},[254,499,500],{},"    system_msgs = [m for m in messages if m['role'] == 'system']\n",[254,502,503],{"class":256,"line":275},[254,504,505],{},"    other_msgs = [m for m in messages if m['role'] != 'system']\n",[254,507,508],{"class":256,"line":281},[254,509,284],{},[254,511,512],{"class":256,"line":287},[254,513,514],{},"    # Keep recent messages within window size\n",[254,516,517],{"class":256,"line":293},[254,518,519],{},"    available_space = window_size - len(system_msgs)\n",[254,521,522],{"class":256,"line":299},[254,523,524],{},"    recent_msgs = other_msgs[-available_space:]\n",[254,526,527],{"class":256,"line":305},[254,528,284],{},[254,530,531],{"class":256,"line":311},[254,532,533],{},"    return system_msgs + recent_msgs\n",[227,535,369,537],{"id":536},"_2-semantic-sliding",[357,538,539],{},"Semantic Sliding",[223,541,542],{},"Instead of purely time-based sliding, keep messages based on relevance:",[232,544,546],{"className":248,"code":545,"language":250,"meta":241,"style":241},"def semantic_sliding(messages, window_size, current_query):\n    # Score messages by relevance to current query\n    scored = [(msg, relevance_score(msg, current_query)) \n              for msg in messages]\n    \n    # Keep most relevant messages\n    scored.sort(key=lambda x: x[1], reverse=True)\n    return [msg for msg, _ in scored[:window_size]]\n",[239,547,548,553,558,563,568,572,577,582],{"__ignoreMap":241},[254,549,550],{"class":256,"line":257},[254,551,552],{},"def semantic_sliding(messages, window_size, current_query):\n",[254,554,555],{"class":256,"line":263},[254,556,557],{},"    # Score messages by relevance to current query\n",[254,559,560],{"class":256,"line":269},[254,561,562],{},"    scored = [(msg, relevance_score(msg, current_query)) \n",[254,564,565],{"class":256,"line":275},[254,566,567],{},"              for msg in messages]\n",[254,569,570],{"class":256,"line":281},[254,571,284],{},[254,573,574],{"class":256,"line":287},[254,575,576],{},"    # Keep most relevant messages\n",[254,578,579],{"class":256,"line":293},[254,580,581],{},"    scored.sort(key=lambda x: x[1], reverse=True)\n",[254,583,584],{"class":256,"line":299},[254,585,586],{},"    return [msg for msg, _ in scored[:window_size]]\n",[227,588,379,590],{"id":589},"_3-hybrid-approach",[357,591,592],{},"Hybrid Approach",[223,594,595],{},"Combine sliding with other strategies:",[419,597,598,601,604],{},[422,599,600],{},"Keep system messages always",[422,602,603],{},"Slide user/assistant messages",[422,605,606],{},"Periodically summarize older content",[218,608,610],{"id":609},"summary","Summary",[223,612,613],{},"Sliding window is simple and efficient but can lead to significant context loss. It's best suited for:",[419,615,616,619,622],{},[422,617,618],{},"Short conversations",[422,620,621],{},"Stateless operations",[422,623,624],{},"Situations where recent context is most important",[223,626,627],{},"For longer, more complex conversations, consider combining sliding windows with other context management strategies.",[629,630,631],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":241,"searchDepth":263,"depth":263,"links":633},[634,639,649,657,665],{"id":220,"depth":263,"text":221,"children":635},[636,637,638],{"id":229,"depth":269,"text":230},{"id":244,"depth":269,"text":245},{"id":340,"depth":269,"text":341},{"id":350,"depth":263,"text":351,"children":640},[641,643,645,647],{"id":354,"depth":269,"text":642},"1. Loss of Critical Context",{"id":368,"depth":269,"text":644},"2. Topic Drift",{"id":378,"depth":269,"text":646},"3. Repetitive Questions",{"id":391,"depth":269,"text":648},"4. Loss of Instructions",{"id":407,"depth":263,"text":408,"children":650},[651,653,655],{"id":411,"depth":269,"text":652},"1. Short Conversations",{"id":433,"depth":269,"text":654},"2. Stateless Operations",{"id":453,"depth":269,"text":656},"3. Real-time Chat",{"id":473,"depth":263,"text":474,"children":658},[659,661,663],{"id":477,"depth":269,"text":660},"1. Priority Sliding",{"id":536,"depth":269,"text":662},"2. Semantic Sliding",{"id":589,"depth":269,"text":664},"3. Hybrid Approach",{"id":609,"depth":263,"text":610},"How sliding context windows work and their limitations.","md",null,{},{"icon":98},{"title":95,"description":666},"W0vcP_1hqDlDBYTUG-Zp_GbpwVZqENho358rEXF1dhU",[674,676],{"title":90,"path":91,"stem":92,"description":675,"icon":93,"children":-1},"Understanding the importance and challenges of context windows in LLMs.",{"title":100,"path":101,"stem":102,"description":677,"icon":103,"children":-1},"Dropping older messages based on max_token limits and their challenges.",1772944811179]