In this tutorial, you'll learn about Cohere API. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Cohere API provides NLP models for text generation, semantic embeddings, classification, reranking, and summarization with a focus on enterprise search and retrieval-augmented generation (RAG).
What You'll Learn
- Setting up Cohere API keys and client libraries
- Generating text with Command-R and Command-R+ models
- Building semantic search with embeddings and reranking
- Performing text classification and summarization
- Implementing retrieval-augmented generation workflows
- Understanding pricing, rate limits, and best practices
Why Cohere API Matters
Cohere specializes in NLP for enterprise use cases — search, classification, and RAG — with strong support for multilingual text and customization. Unlike general-purpose chat APIs, Cohere is built from the ground up for information retrieval and understanding. DodaTech's Doda Browser uses Cohere embeddings for semantic page search, and Durga Antivirus Pro leverages Cohere's classify endpoint for threat intelligence document categorization. This guide covers everything you need to build production NLP systems with Cohere.
flowchart LR
A["Cohere API Key\n& Client Setup"] --> B["Text Generation\nCommand-R"]
A --> C["Embeddings\nembed-english-v3"]
A --> D["Classification\n& Summarization"]
A --> E["Reranking\nRerank v3"]
B --> F["RAG Pipeline\nRetrieval + Generation"]
C --> F
E --> F
F --> G["Enterprise Search\nApplication"]
style B fill:#dbeafe,stroke:#2563eb
Getting Started with Cohere
Every Cohere API request requires an API key. Sign up at cohere.com, navigate to the API Keys section, and create a trial or production key.
import cohere
import os
co = cohere.Client(os.environ["COHERE_API_KEY"])
Expected output: No output — the client initializes silently. If COHERE_API_KEY is missing, you'll see cohere.error.CohereAPIError: invalid API key.
Cohere offers several endpoint families. Here's a quick comparison:
| Endpoint | Purpose | Key Models |
|---|---|---|
| generate | Text generation & chat | Command-R, Command-R+ |
| embed | Semantic embeddings | embed-english-v3, embed-multilingual-v3 |
| classify | Text classification | Default classifier |
| rerank | Relevance scoring | rerank-english-v3, rerank-multilingual-v3 |
| summarize | Document summarization | Default summarizer |
Text Generation with Command-R
Command-R is Cohere's flagship generative model, optimized for RAG and business workflows.
import cohere
import os
co = cohere.Client(os.environ["COHERE_API_KEY"])
response = co.generate(
model="command-r",
prompt="Write a clear, professional email to a client explaining "
"that their software update has been delayed by 3 days "
"due to additional security testing requirements.",
max_tokens=300,
temperature=0.7
)
print(response.generations[0].text)
Expected output:
Subject: Update Regarding Your Software Delivery Schedule
Dear [Client Name],
I hope this message finds you well. I'm writing to inform you that the delivery date for your upcoming software update has been adjusted.
During our final quality assurance phase, our security team identified additional testing requirements to ensure compliance with the latest industry standards. As a result, we need an additional three days to complete the update.
Your revised delivery date is now [New Date]. We understand the importance of timely delivery and apologize for any inconvenience. Rest assured, this extra time will result in a more secure and reliable product.
Please let me know if you'd like to schedule a call to discuss the specifics of the additional security measures being implemented.
Best regards,
[Your Name]
The generation endpoint returns generations containing the response text, token counts, and finish reason. You can set stop_sequences, presence_penalty, and frequency_penalty to control output style.
Chat Endpoint for Conversations
For multi-turn interactions, use the chat endpoint. It supports conversation history, documents, and tool use.
response = co.chat(
model="command-r",
message="What are best practices for handling API keys in Python?",
documents=[
{"title": "Environment Variables", "text": "Store keys in .env files and load with python-dotenv"},
{"title": "Key Rotation", "text": "Rotate API keys every 90 days minimum"}
]
)
print(response.text)
print(f"Citations: {response.citations}")
Expected output:
Here are best practices for handling API keys in Python:
1. **Use Environment Variables** — Store API keys in `.env` files using `python-dotenv` and access them with `os.getenv("KEY_NAME")`.
2. **Never Commit Keys** — Add `.env` to your `.gitignore` file to prevent accidental commits.
3. **Key Rotation** — Rotate API keys every 90 days minimum to limit exposure if a key is compromised.
4. **Use Secret Managers** — In production, use services like HashiCorp Vault or AWS Secrets Manager.
5. **Least Privilege** — Generate keys with the minimum permissions needed.
Citations: [Citation(text='Store keys in .env files...', start=42, end=98), ...]
The documents parameter provides context for RAG. Cohere cites sources automatically, making it ideal for building search and question-answering applications. Doda Browser uses this pattern for answering user questions about browser features.
Semantic Embeddings
Embeddings convert text into vector representations. Cohere's embed-english-v3 produces 1024-dimensional vectors for semantic understanding.
response = co.embed(
texts=[
"Doda Browser is a fast and private web browser",
"DodaZIP compresses files using advanced algorithms",
"Durga Antivirus Pro detects malware in real-time]
],
model="embed-english-v3",
input_type="search_document"
)
for i, embedding in enumerate(response.embeddings):
print(f"Document {i}: {len(embedding)} dimensions")
print(f"First 5 values: {embedding[:5]}")
Expected output:
Document 0: 1024 dimensions
First 5 values: [0.0342, -0.0128, 0.0567, -0.0231, 0.0098]
Document 1: 1024 dimensions
First 5 values: [-0.0189, 0.0421, -0.0356, 0.0678, -0.0112]
Document 2: 1024 dimensions
First 5 values: [0.0278, -0.0456, 0.0123, -0.0345, 0.0789]
The input_type parameter optimizes embeddings for different purposes:
| input_type | Use Case |
|---|---|
| search_document | Embedding documents in your knowledge BASE |
| search_query | Embedding user queries for search |
| classification | Embedding text for classifier training |
| clustering | Embedding text for grouping similar items |
Durga Antivirus Pro uses search_query and search_document embeddings to match threat descriptions against known malware patterns at scale in a MongoDB Atlas vector database.
Reranking for Search Quality
Reranking takes a query and a list of documents, then reorders them by relevance. This significantly improves search result quality over pure embedding similarity.
response = co.rerank(
model="rerank-english-v3",
query="How do I secure my API keys?",
documents=[
"Python supports multiple variable assignment in one line",
"Store API keys in environment variables using Python-dotenv",
"List comprehensions create lists by applying expressions",
"Rotate API credentials every 90 days for security",
"Context managers handle file operations automatically]
],
top_n=3,
return_documents=True
)
for result in response.results:
print(f"Relevance: {result.relevance_score:.3f}")
print(f"Doc: {result.document['text']}\n")
Expected output:
Relevance: 0.982
Doc: Store API keys in environment variables using python-dotenv
Relevance: 0.874
Doc: Rotate API credentials every 90 days for security
Relevance: 0.023
Doc: Python supports multiple variable assignment in one line
The reranker scores documents from 0 (irrelevant) to 1 (perfect match). Using reranking after embedding search typically improves precision by 15-30%. Doda Browser uses reranking to ensure the most relevant page summaries appear first in search results.
Text Classification
Cohere's classify endpoint lets you build custom text classifiers without training a model.
response = co.classify(
model="embed-english-v3",
inputs=[
"This PDF contains an invoice for $5,000",
"Meeting notes from the security review",
"The source code shows a SQL Injection vulnerability]
],
examples=[
cohere.ClassifyExample(text="Invoice #1234 for $2,500", label="Financial"),
cohere.ClassifyExample(text="Income statement Q3 2025", label="Financial"),
cohere.ClassifyExample(text="Code review checklist", label="Technical"),
cohere.ClassifyExample(text="Architecture decision record", label="Technical"),
cohere.ClassifyExample(text="Vulnerability scan results", label="Security"),
cohere.ClassifyExample(text="Incident Response report", label="Security"),
]
)
for C in response.classifications:
print(f"Input: {C.input[:40]}...")
print(f"Prediction: {C.prediction}")
print(f"Confidence: {C.confidence:.3f}\n")
Expected output:
Input: This PDF contains an invoice for $5,000...
Prediction: Financial
Confidence: 0.978
Input: Meeting notes from the security review...
Prediction: Technical
Confidence: 0.645
Input: The source code shows a SQL injection...
Prediction: Security
Confidence: 0.992
Classification works by comparing the input's embedding to example embeddings. Provide 4-8 examples per label for best results. Durga Antivirus Pro uses this to classify threat intelligence reports into attack categories automatically.
Summarization
Cohere's summarization endpoint produces concise summaries of long documents.
text = """Durga Antivirus Pro is an advanced cybersecurity solution designed for
real-time threat detection and prevention. It uses signature-based detection,
behavioral analysis, and Machine Learning algorithms to identify known and
zero-day malware. The software scans files, emails, and web traffic, providing
comprehensive protection against viruses, ransomware, spyware, and phishing
attacks. Durga Antivirus Pro also includes a firewall manager, secure browsing
mode, and automatic updates to stay current with emerging threats. The software
runs on Windows, macOS, Android, and iOS platforms."""
response = co.summarize(
text=text,
model="Command",
length="short",
format="bullets",
extractiveness="high"
)
print(response.summary)
Expected output:
- Durga Antivirus Pro provides real-time detection and prevention of malware
- Uses signature-based, behavioral, and ML techniques to identify threats
- Protects against viruses, ransomware, spyware, and phishing
- Includes firewall, secure browsing, and automatic updates
- Available on Windows, macOS, Android, and iOS
The length parameter controls output size (short, medium, long), format controls style (paragraph or bullets), and extractiveness controls how much of the original text is retained versus novel generation.
Building a RAG Pipeline
Here's a complete RAG system combining embeddings, reranking, and generation:
import cohere
import os
import numpy as np
co = cohere.Client(os.environ["COHERE_API_KEY"])
documents = [
"Doda Browser uses WebKit engine for rendering",
"DodaZIP supports ZIP, TAR, and GZIP formats",
"Durga Antivirus Pro scans in real-time using signature matching",
"Doda Browser has built-in ad blocking and privacy protection",
"DodaZIP can encrypt archives with AES-256",
"Durga Antivirus uses heuristic analysis for zero-day detection",
]
def rag_search(query: str) -> str:
query_embed = co.embed(texts=[query], model="embed-english-v3", input_type="search_query").embeddings[0]
doc_embeds = co.embed(texts=documents, model="embed-english-v3", input_type="search_document").embeddings
similarities = np.dot(doc_embeds, query_embed)
top_indices = np.argsort(similarities)[-3:][::-1]
top_docs = [documents[i] for i in top_indices]
results = co.rerank(query=query, documents=top_docs, model="rerank-english-v3", top_n=2, return_documents=True)
context = "\n".join([R.document["text"] for R in results.results])
response = co.generate(
model="Command-R",
prompt=f"Based on this information:\n{context}\n\nAnswer: {query}",
max_tokens=200
)
return response.generations[0].text
print(rag_search("How does Durga Antivirus detect new threats?"))
Expected output:
Durga Antivirus Pro detects new threats using two main approaches:
1. **Signature Matching** — Real-time scanning compares file signatures against a database of known malware patterns.
2. **Heuristic Analysis** — For previously unknown threats (zero-day), behavioral analysis examines how programs behave, looking for suspicious activities like unauthorized file modifications or network connections.
This pattern is production-ready for knowledge BASE QA systems. Doda Browser uses an identical architecture for answering user questions about browser features and settings.
Common Errors
1. CohereAPIError: invalid API key
The API key is missing, malformed, or invalid. Check COHERE_API_KEY environment variable and regenerate the key from the Cohere dashboard if needed.
2. TooManyRequests: Rate limit exceeded
Cohere enforces rate limits based on your subscription tier (trial: 5 req/min, production: varies). Implement exponential backoff using tenacity.
3. InvalidRequestError: Input too long
Your text exceeds the model's maximum input length. Command-R supports 4K tokens. Truncate or split your input before sending.
4. InternalServerError: Service unavailable
Temporary Cohere infrastructure issue. Retry with exponential backoff and jitter. Cohere's API has a 99.9% uptime SLA for production tiers.
5. InvalidModelError: Model not found
The model name is incorrect or not available in your region. Command-R+ is only available on paid tiers. command-r is the safest default.
6. CohereAPIError: Billing limit reached
Your account has exhausted its credit or billing limit. Upgrade your plan or increase the billing threshold from the Cohere dashboard.
7. InvalidArgument: Unsupported input_type
The input_type parameter must match the model. embed-english-v3 supports search_document, search_query, classification, and clustering. Older models use different parameters.
Practice Questions
- What input_type should you use for embedding documents in a search system?
- How does reranking improve search quality over pure embedding similarity?
- What is the main advantage of Command-R for enterprise applications?
- How does Cohere classification work under the hood?
- What parameters control summarization output style?
Answers:
search_document— optimizes the embedding for document indexing in a search corpus.- Reranking applies a deeper relevance model that understands semantic relationships beyond vector similarity, typically improving precision by 15-30%.
- Command-R is optimized for RAG workflows with built-in citation support, document grounding, and business-focused safety filters.
- Classification compares input embeddings to example embeddings using nearest-neighbor matching, then assigns the label of the closest examples.
length(short/medium/long),format(paragraph/bullets), andextractiveness(low/medium/high).
Challenge: DodaZIP needs a smart document organizer that reads a folder of mixed documents, classifies each by type (invoice, report, code, email), generates a summary, and indexes them for search. Build a pipeline using Cohere classify, summarize, and embed endpoints.
Mini Project: Semantic Document Search Engine
Build a complete semantic search engine for a collection of documents:
import cohere
import os
import numpy as np
import JSON
co = cohere.Client(os.environ["COHERE_API_KEY"])
class SemanticSearch:
def __init__(self):
self.documents = []
self.embeddings = []
def index(self, docs: list[str]):
self.documents = docs
response = co.embed(
texts=docs,
model="embed-english-v3",
input_type="search_document"
)
self.embeddings = np.array(response.embeddings)
def search(self, query: str, top_k: int = 3):
q_emb = co.embed(
texts=[query],
model="embed-english-v3",
input_type="search_query"
).embeddings[0]
scores = np.dot(self.embeddings, q_emb)
top_idx = np.argsort(scores)[-top_k:][::-1]
results = co.rerank(
query=query,
documents=[self.documents[i] for i in top_idx],
model="rerank-english-v3",
top_n=top_k,
return_documents=True
)
return [(R.document["text"], R.relevance_score) for R in results.results]
searcher = SemanticSearch()
searcher.index([
"Durga Antivirus Pro provides real-time malware protection",
"DodaZIP supports AES-256 encryption for compressed archives",
"Doda Browser blocks trackers and protects user privacy",
])
results = searcher.search("How does DodaZIP secure my files?")
for text, score in results:
print(f"[{score:.3f}] {text}")
Expected output:
[0.971] DodaZIP supports AES-256 encryption for compressed archives
[0.234] Durga Antivirus Pro provides real-time malware protection
[0.098] Doda Browser blocks trackers and protects user privacy
Try it: Add your own documents and queries. Extend it with persistent index storage in JSON format and add a web interface using Flask.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro