Documentation Lookup
Retrieve current documentation and code examples for any library using the Context7 CLI.
Make sure the CLI is up to date before running commands:
CODEBLOCK0
Or run directly without installing:
CODEBLOCK1
Workflow
Two-step process: resolve the library name to an ID, then query docs with that ID.
CODEBLOCK2
You MUST call ctx7 library first to obtain a valid library ID UNLESS the user explicitly provides a library ID in the format /org/project or /org/project/version.
IMPORTANT: Do not run these commands more than 3 times per question. If you cannot find what you need after 3 attempts, use the best result you have.
Step 1: Resolve a Library
Resolves a package/product name to a Context7-compatible library ID and returns matching libraries.
CODEBLOCK3
Always pass a query argument — it is required and directly affects result ranking. Use the user's intent to form the query, which helps disambiguate when multiple libraries share a similar name. Do not include any sensitive or confidential information such as API keys, passwords, credentials, personal data, or proprietary code in your query.
Result fields
Each result includes:
- - Library ID — Context7-compatible identifier (format:
/org/project) - Name — Library or package name
- Description — Short summary
- Code Snippets — Number of available code examples
- Source Reputation — Authority indicator (High, Medium, Low, or Unknown)
- Benchmark Score — Quality indicator (100 is the highest score)
- Versions — List of versions if available. Use one of those versions if the user provides a version in their query. The format is
/org/project/version.
Selection process
- 1. Analyze the query to understand what library/package the user is looking for
- Select the most relevant match based on:
- Name similarity to the query (exact matches prioritized)
- Description relevance to the query's intent
- Documentation coverage (prioritize libraries with higher Code Snippet counts)
- Source reputation (consider libraries with High or Medium reputation more authoritative)
- Benchmark score (higher is better, 100 is the maximum)
- 3. If multiple good matches exist, acknowledge this but proceed with the most relevant one
- If no good matches exist, clearly state this and suggest query refinements
- For ambiguous queries, request clarification before proceeding with a best-guess match
Version-specific IDs
If the user mentions a specific version, use a version-specific library ID:
CODEBLOCK4
The available versions are listed in the ctx7 library output. Use the closest match to what the user specified.
Step 2: Query Documentation
Retrieves up-to-date documentation and code examples for the resolved library.
CODEBLOCK5
Writing good queries
The query directly affects the quality of results. Be specific and include relevant details. Do not include any sensitive or confidential information such as API keys, passwords, credentials, personal data, or proprietary code in your query.
| Quality | Example |
|---|
| Good | INLINECODE7 |
| Good |
"React useEffect cleanup function with async operations" |
| Bad |
"auth" |
| Bad |
"hooks" |
Use the user's full question as the query when possible, vague one-word queries return generic results.
The output contains two types of content: code snippets (titled, with language-tagged blocks) and info snippets (prose explanations with breadcrumb context).
Authentication
Works without authentication. For higher rate limits:
CODEBLOCK6
Error Handling
If a command fails with a quota error ("Monthly quota reached" or "quota exceeded"):
- 1. Inform the user their Context7 quota is exhausted
- Suggest they authenticate for higher limits: INLINECODE11
- If they cannot or choose not to authenticate, answer from training knowledge and clearly note it may be outdated
Do not silently fall back to training data — always tell the user why Context7 was not used.
Common Mistakes
- - Library IDs require a
/ prefix — /facebook/react not INLINECODE14 - Always run
ctx7 library first — ctx7 docs react "hooks" will fail without a valid ID - Use descriptive queries, not single words —
"React useEffect cleanup function" not INLINECODE18 - Do not include sensitive information (API keys, passwords, credentials) in queries
文档查找
使用Context7 CLI检索任何库的最新文档和代码示例。
在运行命令前,请确保CLI已更新至最新版本:
bash
npm install -g ctx7@latest
或直接运行而不安装:
bash
npx ctx7@latest
工作流程
两步流程:先将库名称解析为ID,然后使用该ID查询文档。
bash
第一步:解析库ID
ctx7 library
第二步:查询文档
ctx7 docs
除非用户明确提供格式为/org/project或/org/project/version的库ID,否则您必须先调用ctx7 library来获取有效的库ID。
重要提示:每个问题中运行这些命令的次数不要超过3次。如果3次尝试后仍无法找到所需内容,请使用已有的最佳结果。
第一步:解析库
将包/产品名称解析为Context7兼容的库ID,并返回匹配的库。
bash
ctx7 library react How to clean up useEffect with async operations
ctx7 library nextjs How to set up app router with middleware
ctx7 library prisma How to define one-to-many relations with cascade delete
始终传递query参数——这是必需的,且直接影响结果排名。使用用户的意图来构建查询,这有助于在多个库名称相似时进行区分。不要在查询中包含任何敏感或机密信息,如API密钥、密码、凭证、个人数据或专有代码。
结果字段
每个结果包含:
- - 库ID — Context7兼容标识符(格式:/org/project)
- 名称 — 库或包名称
- 描述 — 简短摘要
- 代码片段 — 可用代码示例数量
- 来源信誉 — 权威性指标(高、中、低或未知)
- 基准评分 — 质量指标(100为最高分)
- 版本 — 可用版本列表。如果用户在查询中提供了版本,请使用其中一个版本。格式为/org/project/version。
选择过程
- 1. 分析查询以了解用户正在寻找的库/包
- 基于以下条件选择最相关的匹配:
- 与查询的名称相似度(精确匹配优先)
- 描述与查询意图的相关性
- 文档覆盖范围(优先选择代码片段数量较多的库)
- 来源信誉(认为信誉高或中等的库更权威)
- 基准评分(越高越好,100为最高分)
- 3. 如果存在多个良好匹配,请确认这一点,但继续使用最相关的一个
- 如果没有良好匹配,请明确说明并建议优化查询
- 对于模糊查询,在继续使用最佳猜测匹配之前,请求澄清
版本特定ID
如果用户提到特定版本,请使用版本特定的库ID:
bash
通用(最新索引)
ctx7 docs /vercel/next.js How to set up app router
版本特定
ctx7 docs /vercel/next.js/v14.3.0-canary.87 How to set up app router
可用版本在ctx7 library的输出中列出。使用与用户指定版本最接近的匹配。
第二步:查询文档
检索已解析库的最新文档和代码示例。
bash
ctx7 docs /facebook/react How to clean up useEffect with async operations
ctx7 docs /vercel/next.js How to add authentication middleware to app router
ctx7 docs /prisma/prisma How to define one-to-many relations with cascade delete
编写良好的查询
查询直接影响结果质量。请具体说明并包含相关细节。不要在查询中包含任何敏感或机密信息,如API密钥、密码、凭证、个人数据或专有代码。
| 质量 | 示例 |
|---|
| 良好 | How to set up authentication with JWT in Express.js |
| 良好 |
React useEffect cleanup function with async operations |
| 差 | auth |
| 差 | hooks |
尽可能使用用户的完整问题作为查询,模糊的单词查询会返回通用结果。
输出包含两种类型的内容:代码片段(带标题,有语言标记的代码块)和信息片段(带面包屑导航的说明性文字)。
身份验证
无需身份验证即可使用。如需更高的速率限制:
bash
选项A:环境变量
export CONTEXT7APIKEY=your_key
选项B:OAuth登录
ctx7 login
错误处理
如果命令因配额错误失败(已达到月度配额或超出配额):
- 1. 告知用户其Context7配额已用尽
- 建议他们进行身份验证以获得更高限制:ctx7 login
- 如果他们无法或选择不进行身份验证,则根据训练知识回答,并明确说明可能已过时
不要静默地回退到训练数据——始终告知用户未使用Context7的原因。
常见错误
- - 库ID需要/前缀——/facebook/react而非facebook/react
- 始终先运行ctx7 library——没有有效ID的情况下运行ctx7 docs react hooks会失败
- 使用描述性查询,而非单个单词——React useEffect cleanup function而非hooks
- 不要在查询中包含敏感信息(API密钥、密码、凭证)