# Summarizing Text with the Chat Endpoint

> Learn how to perform text summarization using Cohere's Chat endpoint with features like length control and RAG.

Text summarization distills essential information and generates concise snippets from dense documents. With Cohere, you can do text summarization via the Chat endpoint.

The Command R family of models (R and R+) supports 128k context length, so you can pass long documents to be summarized.

## Basic summarization

You can perform text summarization with a simple prompt asking the model to summarize a piece of text.

<CodeBlocks>
  ```python PYTHON
  import cohere

  co = cohere.ClientV2(api_key="<YOUR API KEY>")

  document = """Equipment rental in North America is predicted to "normalize" going into 2024,
  according to Josh Nickell, vice president of equipment rental for the American Rental
  Association (ARA).
  "Rental is going back to 'normal,' but normal means that strategy matters again -
  geography matters, fleet mix matters, customer type matters," Nickell said. "In
  late 2020 to 2022, you just showed up with equipment and you made money.
  "Everybody was breaking records, from the national rental chains to the smallest
  rental companies; everybody was having record years, and everybody was raising
  prices. The conversation was, 'How much are you up?' And now, the conversation
  is changing to 'What's my market like?'"
  Nickell stressed this shouldn't be taken as a pessimistic viewpoint. It's simply
  coming back down to Earth from unprecedented circumstances during the time of Covid.
  Rental companies are still seeing growth, but at a more moderate level."""

  message = f"Generate a concise summary of this text\n{document}"

  response = co.chat(
      model="command-a-03-2025",
      messages=[{"role": "user", "content": message}],
  )


  print(response.message.content[0].text)
  ```

  ```bash cURL
  curl --request POST \
    --url https://api.cohere.ai/v2/chat \
    --header 'accept: application/json' \
    --header 'content-type: application/json' \
    --header "Authorization: bearer $CO_API_KEY" \
    --data '{
    "model": "command-a-03-2025",
    "messages": [
      {
        "role": "user",
        "content": "Generate a concise summary of this text\n\nEquipment rental in North America is predicted to \"normalize\" going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA).\n\"Rental is going back to '\''normal,'\'' but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,\" Nickell said. \"In late 2020 to 2022, you just showed up with equipment and you made money.\n\"Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, '\''How much are you up?'\'' And now, the conversation is changing to '\''What'\''s my market like?'\''\"\nNickell stressed this shouldn'\''t be taken as a pessimistic viewpoint. It'\''s simply coming back down to Earth from unprecedented circumstances during the time of Covid. Rental companies are still seeing growth, but at a more moderate level."
      }
    ]
  }'
  ```
</CodeBlocks>

(NOTE: Here, we are passing the document as a variable, but you can also just copy the document directly into the message and ask Chat to summarize it.)

Here's a sample output:

```
The equipment rental market in North America is expected to normalize by 2024,
according to Josh Nickell of the American Rental Association. This means a shift
from the unprecedented growth of 2020-2022, where demand and prices were high,
to a more strategic approach focusing on geography, fleet mix, and customer type.
Rental companies are still experiencing growth, but at a more moderate and sustainable level.
```

### Length control

You can further control the output by defining the length of the summary in your prompt. For example, you can specify the number of sentences to be generated.

```python PYTHON
message = f"Summarize this text in one sentence\n{document}"

response = co.chat(
    model="command-a-03-2025",
    messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
```

And here's what a sample of the output might look like:

```
The equipment rental market in North America is expected to stabilize in 2024,
with a focus on strategic considerations such as geography, fleet mix, and
customer type, according to Josh Nickell of the American Rental Association (ARA).
```

You can also specify the length in terms of word count.

```python PYTHON
message = f"Summarize this text in less than 10 words\n{document}"

response = co.chat(
    model="command-a-03-2025",
    messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
```

```
Rental equipment supply and demand to balance.
```

(Note: While the model is generally good at adhering to length instructions, due to the nature of LLMs, we do not guarantee that the exact word, sentence, or paragraph numbers will be generated.)

### Format control

Instead of generating summaries as paragraphs, you can also prompt the model to generate the summary as bullet points.

```python PYTHON
message = f"Generate a concise summary of this text as bullet points\n{document}"

response = co.chat(
    model="command-a-03-2025",
    messages=[{"role": "user", "content": message}],
)

print(response.message.content[0].text)
```

```
- Equipment rental in North America is expected to "normalize" by 2024, according to Josh Nickell
  of the American Rental Association (ARA).
- This "normalization" means a return to strategic focus on factors like geography, fleet mix,
  and customer type.
- In the past two years, rental companies easily made money and saw record growth due to the
  unique circumstances of the Covid pandemic.
- Now, the focus is shifting from universal success to varying market conditions and performance.
- Nickell's outlook is not pessimistic; rental companies are still growing, but at a more 
  sustainable and moderate pace.

```

## Grounded summarization

Another approach to summarization is using [retrieval-augmented generation](https://docs.cohere.com/docs/retrieval-augmented-generation-rag) (RAG). Here, you can instead pass the document as a chunk of documents to the Chat endpoint call.

This approach allows you to take advantage of the citations generated by the endpoint, which means you can get a grounded summary of the document. Each grounded summary includes fine-grained citations linking to the source documents, making the response easily verifiable and building trust with the user.

Here is a chunked version of the document. (we don’t cover the chunking process here, but if you’d like to learn more, see this cookbook on [chunking strategies](https://github.com/cohere-ai/cohere-developer-experience/blob/main/notebooks/guides/Chunking_strategies.ipynb).)

```python PYTHON
document_chunked = [
    {
        "data": {
            "text": "Equipment rental in North America is predicted to “normalize” going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA)."
        }
    },
    {
        "data": {
            "text": "“Rental is going back to ‘normal,’ but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,” Nickell said. “In late 2020 to 2022, you just showed up with equipment and you made money."
        }
    },
    {
        "data": {
            "text": "“Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, ‘How much are you up?’ And now, the conversation is changing to ‘What’s my market like?’”"
        }
    },
]
```

It also helps to create a custom system message to prime the model about the task—that it will receive a series of text fragments from a document presented in chronological order.

```python PYTHON
system_message = """## Task and Context
You will receive a series of text fragments from a document that are presented in chronological order. As the assistant, you must generate responses to user's requests based on the information given in the fragments. Ensure that your responses are accurate and truthful, and that you reference your sources where appropriate to answer the queries, regardless of their complexity."""
```

Other than the custom system message, the only change to the Chat endpoint call is passing the document parameter containing the list of document chunks.

Aside from displaying the actual summary, we can display the citations as as well. The citations are a list of specific passages in the response that cite from the documents that the model receives.

<CodeBlocks>
  ```python PYTHON
  message = f"Summarize this text in one sentence."

  response = co.chat(
      model="command-a-03-2025",
      documents=document_chunked,
      messages=[
          {"role": "system", "content": system_message},
          {"role": "user", "content": message},
      ],
  )

  print(response.message.content[0].text)

  if response.message.citations:
      print("\nCITATIONS:")
      for citation in response.message.citations:
          print(
              f"Start: {citation.start} | End: {citation.end} | Text: '{citation.text}'",
              end="",
          )
          if citation.sources:
              for source in citation.sources:
                  print(f"| {source.id}")
  ```

  ```bash cURL
  curl --request POST \
    --url https://api.cohere.ai/v2/chat \
    --header 'accept: application/json' \
    --header 'content-type: application/json' \
    --header "Authorization: bearer $CO_API_KEY" \
    --data '{
    "model": "command-a-03-2025",
    "messages": [
      {
        "role": "system",
        "content": "## Task and Context\nYou will receive a series of text fragments from a document that are presented in chronological order. As the assistant, you must generate responses to user'\''s requests based on the information given in the fragments. Ensure that your responses are accurate and truthful, and that you reference your sources where appropriate to answer the queries, regardless of their complexity."
      },
      {
        "role": "user",
        "content": "Summarize this text in one sentence."
      }
    ],
    "documents": [
      {
        "data": {
          "text": "Equipment rental in North America is predicted to \"normalize\" going into 2024, according to Josh Nickell, vice president of equipment rental for the American Rental Association (ARA)."
        }
      },
      {
        "data": {
          "text": "\"Rental is going back to '\''normal,'\'' but normal means that strategy matters again - geography matters, fleet mix matters, customer type matters,\" Nickell said. \"In late 2020 to 2022, you just showed up with equipment and you made money.\""
        }
      },
      {
        "data": {
          "text": "\"Everybody was breaking records, from the national rental chains to the smallest rental companies; everybody was having record years, and everybody was raising prices. The conversation was, '\''How much are you up?'\'' And now, the conversation is changing to '\''What'\''s my market like?'\''\""
        }
      }
    ]
  }'
  ```
</CodeBlocks>

```
Josh Nickell, vice president of the American Rental Association, predicts that equipment rental in North America will "normalize" in 2024, requiring companies to focus on strategy, geography, fleet mix, and customer type.

CITATIONS:
Start: 0 | End: 12 | Text: 'Josh Nickell'| doc:1:0
Start: 14 | End: 63 | Text: 'vice president of the American Rental Association'| doc:1:0
Start: 79 | End: 112 | Text: 'equipment rental in North America'| doc:1:0
Start: 118 | End: 129 | Text: '"normalize"'| doc:1:0
| doc:1:1
Start: 133 | End: 137 | Text: '2024'| doc:1:0
Start: 162 | End: 221 | Text: 'focus on strategy, geography, fleet mix, and customer type.'| doc:1:1
| doc:1:2
```

## Migration from Summarize to Chat Endpoint

To use the Command R/R+ models for summarization, we recommend using the Chat endpoint. This guide outlines how to migrate from the Summarize endpoint to the Chat endpoint.

```python PYTHON