# Cohere

MillionShort search for Cohere's Command models, as a tool whose results go back as documents the answer cites, in Python.

Recipe · Python · Nothing of ours to install · Checked 2026-11-19

> **For agents.** Read the API key from the `MILLIONSHORT_API_KEY` environment variable. Never print it; where a client needs it in a settings file, use a file that is not committed.
> The MCP address is https://mcp.millionshort.com/mcp (streamable HTTP; send `Authorization: Bearer <key>`, or no header for the no-key mode).
> On `INSUFFICIENT_CREDITS` stop and ask a person; on `RATE_LIMITED` or `CAPACITY` wait the `Retry-After` seconds, then retry once.
> Set-up steps written for agents: https://millionshort.com/docs/auth.md. Every integration: https://millionshort.com/docs/integrations.md.

Cohere's Chat endpoint takes function tools: the model asks for a call, your code runs the search, and you send the results back as `document` items. The model then answers from those documents and returns citations that point at them. Cohere's tool use documentation shows Python, so this page does too.

## 1. Install

```bash Python
pip install cohere requests
export MILLIONSHORT_API_KEY=ms-YOUR_API_KEY
```

The example reads your Cohere key from COHERE_API_KEY. Get a MillionShort key in the [dashboard](https://millionshort.com/app/); an agent can ask for a [trial key](https://millionshort.com/docs/api-reference/agent-key) by itself.

## 2. Minimal example

```python Python
import json
import os

import cohere
import requests


def millionshort_search(query: str) -> list[dict]:
    r = requests.post(
        "https://api.millionshort.com/v1/search",
        headers={"Authorization": f"Bearer {os.environ['MILLIONSHORT_API_KEY']}"},
        json={"query": query, "remove_top_sites": 1000, "num_results": 5},
        timeout=30,
    )
    r.raise_for_status()
    return [{"title": x["title"], "url": x["url"], "snippet": x.get("snippet")} for x in r.json()["data"]["results"]]


tools = [{"type": "function", "function": {
    "name": "millionshort_search",
    "description": "Search the web with MillionShort, leaving out the 1,000 most popular sites.",
    "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]},
}}]
co = cohere.ClientV2(api_key=os.environ["COHERE_API_KEY"])
messages = [{"role": "user", "content": "Find independent blogs about home espresso."}]
response = co.chat(model="command-a-plus-05-2026", messages=messages, tools=tools)
while response.message.tool_calls:
    messages.append(response.message)
    for call in response.message.tool_calls:
        results = millionshort_search(**json.loads(call.function.arguments))
        documents = [{"type": "document", "document": {"data": json.dumps(result)}} for result in results]
        messages.append({"role": "tool", "tool_call_id": call.id, "content": documents})
    response = co.chat(model="command-a-plus-05-2026", messages=messages, tools=tools)
print(response.message.content[0].text)
print(response.message.citations)
```

Each search result becomes one document, so each citation points at one page. The model may call the tool again after seeing results; the loop runs until it answers.

## 3. Tell it when to search

Cohere searches nothing unless your code does, so there is no built-in search to turn off. Put a line like this in a `system` message at the start of `messages`:

> Use the MillionShort `search` tool for anything recent, niche, or that needs a source. To reach lesser-known sites, set `remove_top_sites` (for example 1000). Cite the URLs it returns.

## 4. Check it works

Run the example, then ask:

> Search MillionShort for forum threads about restoring a cast iron skillet, with the 1,000 most popular sites removed. List the five most useful with their links.

After the first `co.chat` call, `response.message.tool_calls` names `millionshort_search` with a `query`; the final answer's `citations` point at the documents you sent back.

## Tools

The function tool above is `search` alone. For the server's other tools as well (`get_domain_info`, `list_sources` and the tools of the other endpoints), use an agent framework that speaks MCP with our [MCP server](https://millionshort.com/docs/mcp):

| Tool | What it does | Without a key | Cost |
|---|---|---|---|
| `search` | Searches the web and the specialist sources, with the popularity, domain, page-type, topic, site and date filters | Yes: one source, up to 5 results | 1 credit per source searched, first 10 results |
| `get_domain_info` | Popularity and risk for up to 25 sites | No | 1 credit |
| `list_sources` | The sources, page types and topics `search` accepts | Yes: source names | Free |
| The tools of the other endpoints | `extract`, `map`, `crawl`, `places` and more: [the MCP page](https://millionshort.com/docs/mcp#tools) lists them | A few, such as `places` | What the API call behind each costs |

## Settings

| Variable | Meaning |
|---|---|
| `MILLIONSHORT_API_KEY` | Your key, read by the example above |
| COHERE_API_KEY | Your Cohere key, read by the example above |

## Costs and limits

A tool call costs what the API call behind it costs: 1 credit per source searched for the first 10 results, 1 credit for a domain lookup, nothing for the list of sources, the price of the endpoint for the other tools, and nothing at all in the no-key mode. An agent that loops can make many calls: give it a step limit, and ask for fewer results when a few will do. [Credits and limits](https://millionshort.com/docs/credits-and-limits) has the numbers.

## When it fails

| What you see | Why | Fix |
|---|---|---|
| `401 AUTH_MISSING_KEY` or `AUTH_INVALID_KEY` | The key is missing or wrong | Set `MILLIONSHORT_API_KEY`; keys start with `ms-` |
| `400 INVALID_PARAM` | A field name or value the API does not take | The `message` names the field; the [search reference](https://millionshort.com/docs/search/parameters) lists them all |
| `429 RATE_LIMITED` or `CAPACITY` | Too many calls a minute, or no free capacity right now | Wait the `Retry-After` seconds, then retry |
| `429 INSUFFICIENT_CREDITS` | The plan's credits are used up | Stop and add credits; do not retry |
| The model answers without calling the tool | Nothing told it when to search | Add the line from step 3 |

## Related

- [Agent tool use](https://millionshort.com/docs/search/guides/agent-tool-use): when to give a model tools, and what to tell it.
- [RAG with MillionShort](https://millionshort.com/docs/search/guides/rag): retrieval with citations, step by step.
- Other model APIs: [Mistral](https://millionshort.com/docs/integrations/mistral), [OpenAI](https://millionshort.com/docs/integrations/openai), [Anthropic](https://millionshort.com/docs/integrations/anthropic), [Ollama](https://millionshort.com/docs/integrations/ollama).
- [Search API reference](https://millionshort.com/docs/search/parameters) and [MCP server](https://millionshort.com/docs/mcp).
- Cohere's own documentation, read on 2026-11-19: [Tool use quickstart](https://docs.cohere.com/docs/tool-use-quickstart), [Tool use overview](https://docs.cohere.com/docs/tool-use-overview).
