# Context

## What is Context?

Context is the background information or history that helps provide meaning to a conversation or statement. In human interactions, context is essential for understanding what someone is saying. For example, if you’re chatting with a friend and they suddenly say, “It’s so expensive,” you know what they mean because of the ongoing conversation—maybe you were just discussing a new smartphone or a restaurant bill. Without context, their statement could seem vague or confusing.

In AI chat tools, context plays a similar role. These tools, like ChatGPT, rely on the information shared earlier in the conversation to interpret and respond to the current question or statement accurately. For instance, if you ask, “What’s the weather like in New York today?” and then later ask, “What about tomorrow?”, the AI uses the context from your first question to understand that "tomorrow" also refers to the weather in New York. Without context, the AI might not know what "tomorrow" refers to, leading to incomplete or incorrect answers.

***

## What is Context Management?

Context management is how an AI tool tracks and uses the ongoing conversation to provide relevant and coherent responses. It involves "remembering" the user's previous messages and integrating that information into its responses. For example, if you ask the AI, “What’s 5 + 3?” and then ask, “Now multiply that by 2,” the AI understands that "that" refers to the result of 5 + 3, which is 8, so it answers with 16.

Proper context management allows the AI to:

1. **Interpret Ambiguous Statements**: If a user says, “Can you help with it?” context management helps the AI figure out what “it” refers to.
2. **Maintain a Natural Flow**: Conversations with context feel more human-like, as the AI can follow the thread of the discussion without needing the user to repeat everything.
3. **Provide Personalization**: In advanced systems, context can include user preferences or prior interactions, enabling more tailored responses.

***

## Challenges in Context Management

While context management sounds straightforward, it is technically complex due to several limitations:

1. **Token Limitations**: AI models like ChatGPT can only "remember" a certain amount of text (tokens) from the current conversation. If the conversation is too long, older messages might be "forgotten," affecting the flow and coherence.
2. **Session Boundaries**: Most AI chat tools can only manage context within a single session. Once the session ends, the context is typically lost unless advanced memory systems are used.
3. **Misinterpretation**: Sometimes, the AI might misinterpret the context or prioritize irrelevant parts of the conversation, leading to errors in responses.

To overcome these challenges, some implementations use external memory systems that store and retrieve past interactions. This approach allows for longer-term context retention, enabling the AI to "remember" user preferences or previous sessions for better personalization and accuracy.

***

## Why is Context Management Important?

Context management is crucial for making AI interactions seamless and effective. Without it, conversations would feel fragmented, and the user would have to repeat information constantly, reducing the usability of the tool. By understanding and managing context, AI chat tools can handle complex queries, follow multi-step instructions, and provide a more engaging and helpful user experience.

### Let's compare to something we know

#### **1. Access to Information:**

* **Open-Book Test:** In an open-book test, students have access to textbooks, notes, or other resources while answering questions. They don’t need to memorize everything; instead, they refer to the book to understand the problem and find relevant information to formulate their answers.
* **AI Context Management:** Similarly, AI chat tools like ChatGPT don’t "memorize" past conversations permanently. Instead, they reference the context provided within the conversation window (the "open book") to construct responses. This context is the running thread of the current session, which includes all the user’s inputs and the AI's outputs up to a certain token limit.

***

#### **2. Focused Problem-Solving:**

* **Open-Book Test:** In a test, the student uses the textbook selectively to focus on specific sections or examples that help answer the given question. They don’t read the entire book again but focus on what’s relevant to the problem.
* **AI Context Management:** AI tools operate in a similar way. They don’t process the entire history of what they’ve learned during training for every question but focus on the active context of the session to generate answers. If you provide a detailed question with relevant details (like looking up a specific page of the book), the AI can give a precise response.

***

#### **3. Limitations of Scope:**

* **Open-Book Test:** While students have access to the book, they still need to work within the constraints of time and their understanding of how to use the book effectively. If they waste time flipping through irrelevant sections, they might not finish the test.
* **AI Context Management:** AI has a limitation on how much context it can "see" at once (e.g., 4096 tokens for some models). If the conversation exceeds this limit, earlier parts of the context may be “forgotten,” just like a student might lose track of earlier sections of the test if they aren’t managing their resources well.

***

#### **4. Personal Knowledge vs. Resource Use:**

* **Open-Book Test:** A student combines their prior knowledge with information from the book to answer questions. The test still evaluates their understanding of concepts and their ability to apply them, even with the book as a reference.
* **AI Context Management:** AI uses its pre-trained knowledge (its "education") along with the specific context provided in the conversation (its "book") to generate responses. For example, if you ask ChatGPT about a mathematical formula, it uses its training to know what formulas exist, but it relies on the context of your question to apply the right one.

***

#### **5. Risk of Misinterpretation:**

* **Open-Book Test:** If the student doesn’t understand the question properly or looks at the wrong section of the book, they might give an incorrect answer, even with access to resources.
* **AI Context Management:** Similarly, if the AI misinterprets the context or focuses on the wrong part of the conversation, it might generate a response that’s irrelevant or incorrect. For example, if the user switches topics without clarification, the AI might still rely on earlier context that no longer applies.

***

#### **6. Efficiency and Strategy:**

* **Open-Book Test:** A successful student knows how to locate information quickly and accurately, using the book efficiently without wasting time. Their strategy involves identifying key parts of the question and finding the relevant section of the book.
* **AI Context Management:** Effective use of AI involves providing clear and concise prompts. If users give too much unnecessary information (analogous to pointing at multiple unrelated sections of the book), the AI might struggle to prioritize the relevant details. Conversely, precise and structured inputs improve the AI's performance.

***

#### **7. Memory and Permanence:**

* **Open-Book Test:** After the test is over, students don’t have access to the book anymore unless they bring it along to another test or study session. Similarly, they may not remember all the answers they wrote unless they studied them beforehand.
* **AI Context Management:** AI does not retain context beyond the current session (unless integrated with a memory system). Once the session ends, the "open book" is closed, and the AI cannot recall what was discussed earlier unless the user provides it again.

#### **AI Context Management: Limited View of the Book**

AI models have a "window" through which they can process information, similar to how a student might have access to only the first 25 pages of a 100-page book during an open-book test. For example, if an AI has a 4,096-token limit, it can only "read" and consider that many tokens of input and output combined. This is akin to being able to consult only a subset of the book at a time.

In this analogy:

* **Smaller Context Windows (e.g., 4,096 tokens):** These are like books where you can only read a few chapters or sections (e.g., the first 25 pages). If the answer lies on page 50, the AI—or the student—will not have access to it unless the relevant section is brought into view (i.e., restated or reformatted to fit within the window).
* **Larger Context Windows (e.g., 10,000 tokens):** Models with more capacity are like having access to 100 pages instead of 25. They can handle much larger conversations or datasets, reducing the need to truncate or summarize information.

***

#### **Implications of Limited Context**

1. **For the AI:**
   * If the conversation or document exceeds the token limit, earlier parts may "fall out" of the viewable window, much like the content on pages 26-100 of a 100-page book becomes inaccessible.
   * To manage this, users might need to provide summaries or repeat important points, akin to writing notes from earlier pages to keep key information in view.
2. **For the User:**
   * Users interacting with an AI need to ensure that critical details fit within the model's context window. If too much unnecessary or irrelevant information is included, the important parts might get pushed out of the window.
   * This is similar to a student flipping through irrelevant sections of the book and running out of time or focus to answer the question.

***

#### **Different Models, Different "Books"**

Just as different students might bring different resources to an exam, different LLMs have varying capacities for context:

* **Smaller Models (eg: 1,000 tokens):** These are like being able to read only a brief excerpt from a book. Suitable for short and simple queries but may struggle with detailed or multi-part questions.
* **Larger Models (eg: 10,000 tokens or more):** These are like having access to nearly the whole book, enabling the AI to consider complex queries, analyze long documents, or maintain coherent conversations over extended interactions.

***

## Large Language Models (LLMs) context window

Large Language Models (LLMs) have varying capacities for processing and retaining information within a single interaction, known as the "context window." This context window is measured in tokens, where one token generally equates to a word or a part of a word. The size of this window determines how much text the model can consider at once, impacting its ability to understand and generate coherent responses, especially in lengthy conversations or documents.

Here's an overview of the context window sizes for several major LLMs:

| Model       | Context Window Size | Details                                                                                                                                                                                                                          |
| ----------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| GPT-3       | 2,048 tokens        | Developed by OpenAI, GPT-3 can process up to 2,048 tokens in a single interaction, allowing it to handle moderately long texts but requiring truncation or summarization for more extensive documents.                           |
| GPT-4       | 8,192 tokens        | The successor to GPT-3, GPT-4 increases the context window to 8,192 tokens, enabling it to manage longer and more complex texts effectively.                                                                                     |
| GPT-4 Turbo | 128,000 tokens      | An enhanced version of GPT-4, GPT-4 Turbo features a substantial context window of up to 128,000 tokens, facilitating the processing of extensive documents or maintaining context over prolonged conversations.                 |
| GPT-4o      | 128,000 tokens      | Introduced by OpenAI, GPT-4o supports a context window of up to 128,000 tokens, allowing for extensive input data processing. This capacity enables the model to handle large datasets and complex tasks without losing context. |
| GPT-4o1     | 128,000 tokens      | The GPT-4o1 model also features a context window of 128,000 tokens, similar to GPT-4o, facilitating the handling of extensive datasets and complex tasks.                                                                        |
| Claude 2.1  | 200,000 tokens      | Developed by Anthropic, Claude 2.1 boasts a substantial context window of up to 200,000 tokens, allowing it to process extensive documents or maintain context over prolonged conversations.                                     |
| Gemini 1.5  | 1,000,000 tokens    | Google's Gemini 1.5 model features an impressive context window of up to 1,000,000 tokens, facilitating the handling of extensive datasets and complex tasks.                                                                    |
| LLaMA 2     | 4,096 tokens        | Released by Meta, LLaMA 2 has a context window of 4,096 tokens, suitable for various applications but with limitations on longer texts.                                                                                          |
| LLaMA 3     | 128,000 tokens      | The latest in the LLaMA series, LLaMA 3 expands the context window to 128,000 tokens, significantly enhancing its ability to process lengthy documents and maintain context in extended interactions.                            |
| Gemma 2     | 2,000,000 tokens    | Google's Gemma 2 model extends the context window beyond 2,000,000 tokens, enabling it to handle extensive and complex datasets effectively.                                                                                     |

These varying context window sizes reflect the models' capabilities in handling different lengths of text and complexity. Larger context windows allow models to maintain coherence over longer interactions and process more extensive documents without losing track of earlier information. However, increasing the context window also demands more computational resources and can introduce challenges in model training and deployment.

It's important to note that while larger context windows provide the ability to handle more information at once, they also require efficient management to ensure relevant information is prioritized, and irrelevant data does not overwhelm the model's processing capacity. As AI technology advances, we can expect further enhancements in context window sizes and the development of more sophisticated methods for managing extensive contexts effectively.

***

## Enhancing Context Management: Detailed Explanation

When working with language models that have limitations on the size of their context windows, certain strategies can help users manage large volumes of information effectively. Below is a detailed explanation of **summarization**, **chunking**, and **external memory systems**, which are key techniques for enhancing context management.

***

#### **1. Summarization**

**What It Is:**\
Summarization involves condensing large documents, conversations, or datasets into shorter, essential points. This ensures that only the most relevant information is included in the AI model’s context window.

**How It Works:**

* **Extractive Summarization:** This method pulls the most important sentences or phrases directly from the original text. For example, in a lengthy article about climate change, the summarizer might extract key sentences discussing global temperature rise and its causes.
* **Abstractive Summarization:** Here, the model rephrases or synthesizes the content to produce a shorter version. Instead of directly quoting, the model might summarize a paragraph in one sentence, offering a higher level of abstraction.

**Applications:**

* Condensing meeting transcripts into key takeaways.
* Reducing multi-page research papers into bullet points for quick reference.
* Summarizing customer queries for faster response in support systems.

**Challenges:**

* Summaries might omit critical context if not done carefully.
* Abstractive summaries can introduce inaccuracies if the AI misunderstands the material.

**Best Practices:**

* Clearly define what aspects of the document are most important before summarizing.
* Use AI tools specifically designed for summarization, such as OpenAI's summarization features or other dedicated summarization APIs.

***

#### **2. Chunking**

**What It Is:**\
Chunking is the process of dividing long texts or datasets into smaller, manageable pieces that can fit within the model’s context window. Each chunk is processed individually, and the results are later combined or referenced.

**How It Works:**

* **Identify Logical Breakpoints:** Break the text at natural divisions, such as paragraphs, sections, or chapters. For example, a 100-page document can be divided by headings or topics.
* **Process Sequentially:** Send each chunk to the AI in sequence, ensuring that the output for one chunk feeds into the next as needed.
* **Overlap for Continuity:** Use overlapping tokens between chunks to maintain continuity. For instance, if the last few sentences of one chunk are relevant to the next, include them at the beginning of the following chunk.

**Applications:**

* Breaking down legal contracts into clauses for detailed analysis.
* Dividing lengthy books or research papers for summarization or content extraction.
* Handling datasets in a step-by-step manner to derive insights.

**Challenges:**

* Maintaining coherence across chunks, especially when the AI lacks access to the full document.
* Time-consuming for very large datasets.

**Best Practices:**

* Use overlap strategies (e.g., repeating key portions of text) to maintain a smooth flow.
* Label or index chunks clearly to enable easy recombination or cross-referencing.

***

#### **3. External Memory Systems**

**What It Is:**\
External memory systems provide a way to "store" information outside the immediate context window of the AI, allowing it to reference or retrieve data on demand. This is particularly useful for tasks involving large or dynamic datasets.

**How It Works:**

* **Vector Databases:** Information is stored as embeddings (numerical representations of text or data). When the AI needs to retrieve information, it searches the database for the most relevant embeddings based on the user query.
* **External Summaries:** Key details or documents are pre-summarized and stored in an accessible format. For example, a knowledge base might hold summaries of all company policies for quick retrieval during customer support interactions.
* **Linked Context Systems:** External tools dynamically feed the AI with context as needed. For instance, if a user asks a question about a specific section of a document, the system retrieves that section and provides it to the AI.

**Applications:**

* Building AI-powered chatbots with access to a company’s entire document repository.
* Developing systems for legal or medical professionals where precision is critical, and the AI retrieves exact passages from case files or medical journals.
* Creating intelligent search engines for large datasets.

**Challenges:**

* Requires additional infrastructure, such as databases or APIs, to manage external memory.
* Slower response times if retrieval processes are not optimized.

**Best Practices:**

* Use high-quality embedding models to ensure accurate retrieval.
* Optimize indexing and query processes for speed and reliability.
* Regularly update the external memory system to include the latest information.

***

#### **Comparison of the Strategies**

| **Aspect**     | **Summarization**                            | **Chunking**                               | **External Memory Systems**                     |
| -------------- | -------------------------------------------- | ------------------------------------------ | ----------------------------------------------- |
| **Scope**      | Condenses a single document or conversation. | Divides lengthy texts into smaller pieces. | Manages large datasets or knowledge bases.      |
| **Complexity** | Relatively simple with summarization tools.  | Moderate, requires logical breakpoints.    | High, requires external infrastructure.         |
| **Use Case**   | Meeting notes, quick reference.              | Books, contracts, research papers.         | Dynamic and large-scale data access.            |
| **Continuity** | Risk of losing details.                      | May lose coherence without overlap.        | Maintains full details but relies on retrieval. |

***

By employing these strategies effectively, users can overcome the limitations of small context windows in AI models, ensuring they extract maximum value while maintaining coherence and relevance in responses.
