# Long-Form Text Strategies with Cohere

> This discusses ways of getting Cohere's LLM platform to perform well in generating long-form text.

<AuthorsContainer
  authors={[
    {
      name: "Ania Bialas",
      imageSrc: "https://fern-image-hosting.s3.amazonaws.com/cohere/c5dc5a3-Ania.jpg",
    },
  ]}
/>

<CookbookHeader href="https://github.com/cohere-ai/cohere-developer-experience/blob/main/notebooks/guides/Long_form_General_Strategies.ipynb" />

Large Language Models (LLMs) are becoming increasingly capable of comprehending text, among others excelling in document analysis. The new Cohere model, [Command A](https://huggingface.co/CohereLabs/c4ai-command-a-03-2025), boasts a context length of 256k, which makes it particularly effective for such tasks. Nevertheless, even with the extended context window, some documents might be too lengthy to accommodate in full.

In this cookbook, we'll explore techniques to address cases when relevant information doesn't fit in the model context window.

We'll show you three potential mitigation strategies: truncating the document, query-based retrieval, and a "text rank" approach we use internally at Cohere.

## Summary

| Approach              | Description                                                                                            | Pros                                                                           | Cons                                                                           | When to use?                                                                               |
| --------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| Truncation            | Truncate the document to fit the context window.                                                       | - Simplicity of implementation<br />(does not rely on extrenal infrastructure) | - Loses information at the end of the document                                 | Utilize when all relevant information is contained<br /> at the beginning of the document. |
| Query Based Retrieval | Utilize semantic similarity to retrieve text chunks<br /> that are most relevant to the query.         | - Focuses on sections directly relevant to<br /> the query                     | - Relies on a semantic similarity algorithm.<br />- Might lose broader context | Employ when seeking specific<br /> information within the text.                            |
| Text Rank             | Apply graph theory to generate a cohesive set<br /> of chunks that effectively represent the document. | - Preserves the broader picture.                                               | - Might lose detailed information.                                             | Utilize in summaries and when the question<br /> requires broader context.                 |

## Getting Started \[#getting-started]

```python PYTHON
%%capture
!pip install cohere
!pip install python-dotenv
!pip install tokenizers
!pip install langchain
!pip install nltk
!pip install networkx
!pip install pypdf2
```

```python PYTHON
import os
import requests
from collections import deque
from typing import List, Tuple

import cohere

import numpy as np

import PyPDF2
from dotenv import load_dotenv

from tokenizers import Tokenizer

import nltk
nltk.download('punkt')  # Download the necessary data for sentence tokenization
from nltk.tokenize import sent_tokenize

import networkx as nx
from getpass import getpass
```

```txt title="Output"
[nltk_data] Downloading package punkt to
[nltk_data]     /home/anna_cohere_com/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
```

```python PYTHON