context7-cli 0.2.6

Search library documentation from your terminal — zero runtime, bilingual (EN/PT), multi-key rotation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# context7-cli LLM Rules and Prompt Templates / Regras e Templates de Prompt

---

## English

### Rules for LLM agents using context7-cli

These rules prevent common mistakes when an LLM agent controls the `context7` CLI. Follow them in every session.

#### Rule 1 — Always discover library IDs with `library` first

```bash
# CORRECT: discover the ID before fetching docs
context7 library react --json
# → use the returned "id" field, e.g., "/reactjs/react.dev"
context7 docs /reactjs/react.dev --query "useEffect"

# WRONG: guessing the ID
context7 docs /react/react     # likely 404
context7 docs react            # will fail
```

#### Rule 2 — Always use `--json` for machine-readable output

```bash
# CORRECT
context7 library react --json | jaq '.[0].id'

# AVOID for scripting (ANSI colors break parsing)
context7 library react
```

#### Rule 3 — Use `--text` when inserting docs into LLM context

```bash
# CORRECT: plain text, no ANSI, clean for context windows
context7 docs /reactjs/react.dev --query "hooks" --text

# AVOID: JSON is verbose; use only when you need the snippet structure
context7 docs /reactjs/react.dev --query "hooks" --json
```

#### Rule 4 — Never expose full API keys in prompts or logs

```bash
# CORRECT: list shows masked values
context7 keys list
# Output: 1. ctx7sk-abcd...xyz9

# AVOID: export shows full values — only use in secure environments
context7 keys export
```

#### Rule 5 — Respect trust scores

- Trust score range: 0–10
- Score ≥ 8: high confidence — use directly
- Score 5–7: medium confidence — verify with the user before using
- Score < 5: low confidence — flag it, prefer another source

#### Rule 6 — Handle errors without crashing the workflow

| Error message | Cause | Agent action |
|---------------|-------|-------------|
| `No API key found` | No key configured | Inform the user; ask them to run `context7 keys add ctx7sk-...` |
| `401 Unauthorized` | Invalid key | Ask user to check and refresh key at context7.com |
| `429 Too Many Requests` | Rate limit (after up to 5 attempts) | Wait 10–30 seconds; suggest adding more keys |
| `Network error` | No internet or timeout | Report to user; do not loop indefinitely |
| `400 Bad Request` | Malformed query | Sanitize the query and retry once |

#### Rule 7 — Mutually exclusive flags

`--text` and `--json` cannot be combined. The CLI will return an error if both are passed.

```bash
# WRONG
context7 docs /reactjs/react.dev --text --json

# CORRECT
context7 docs /reactjs/react.dev --text
# or
context7 docs /reactjs/react.dev --json
```

#### Rule 8 — Language selection (v0.2.0+)

Use `--lang` or `CONTEXT7_LANG` to control the UI language in multilingual pipelines:

```bash
# Force English for consistent parsing in scripts
context7 --lang en library react --json

# Set permanently for a session
export CONTEXT7_LANG=en

# Portuguese output for Brazilian users
context7 --lang pt docs /reactjs/react.dev --query "hooks" --text
```

Auto-detect order: `--lang` flag → `CONTEXT7_LANG` env var → system locale (any `pt*` locale → Portuguese) → English default.

---

### Prompt templates

#### Template 1 — Library discovery

Use this when the user mentions a library by name and you need to find its Context7 ID:

```
I need to find documentation for the library "<LIBRARY_NAME>".

Step 1: run this command and parse the JSON output:
```bash
context7 library <LIBRARY_NAME> --json
```

From the response array, select the entry with the highest `trustScore`.
Present the top 3 results to the user in this format:
- `<id>`: <title> (trust score: <score>)

Ask the user: "Which of these libraries would you like documentation for?"

Once they confirm, proceed to Template 2 with the selected `id`.
```

#### Template 2 — Documentation fetch

Use this after you have the library ID from Template 1:

```
I will now fetch the documentation for `<LIBRARY_ID>` about: <TOPIC>.

Run:
```bash
context7 docs <LIBRARY_ID> --query "<TOPIC>" --text
```

Insert the entire output into the conversation context.
If the output is longer than 4000 tokens, summarize the most relevant sections.
Always cite the `codeId` field (source URL) provided in the output.
```

#### Template 3 — API key management guidance

Use when the user needs to set up or troubleshoot API keys:

```
To configure context7-cli API keys, run these commands:

1. Add your API key (get one at https://context7.com):
```bash
context7 keys add ctx7sk-YOUR-KEY-HERE
```

2. Verify it was stored:
```bash
context7 keys list
```

3. (Optional) Add more keys for better rate limit handling:
```bash
context7 keys add ctx7sk-SECOND-KEY
context7 keys add ctx7sk-THIRD-KEY
```

4. Check where the config file is stored:
```bash
context7 keys path
```

5. (Optional) Check current key status:
```bash
context7 keys list
# Rotation is automatic — the CLI shuffles keys per request
```
```

#### Template 4 — Multi-library comparison

Use when the user wants to compare documentation across multiple libraries:

```
I will fetch documentation for multiple libraries to compare them.

For each library in the list, run:
```bash
context7 docs <library-id> --query "<TOPIC>" --text
```

Present the results side by side with headers:
## <Library Name> (<library-id>)
<documentation content>

After presenting all results, summarize the key differences.
```

#### Template 5 — Pipeline: search + fetch + summarize

Use for automated documentation research sessions:

```
Documentation research session for topic: "<TOPIC>"

Step 1 — Find relevant libraries:
```bash
context7 library <TOPIC> --json
```
Select the top 3 results by trustScore.

Step 2 — Fetch docs for each:
```bash
context7 docs <id1> --query "<TOPIC>" --text
context7 docs <id2> --query "<TOPIC>" --text
context7 docs <id3> --query "<TOPIC>" --text
```

Step 3 — Synthesize: provide a single summary that covers:
- What each library does
- How they handle <TOPIC>
- Key differences and recommendations
```

---

### Integration with shell scripts

**Bash script: search and save documentation**

```bash
#!/usr/bin/env bash
set -euo pipefail

LIBRARY="${1:?Usage: $0 <library-name> [query]}"
QUERY="${2:-}"
OUTPUT_DIR="${3:-/tmp/context7-docs}"

mkdir -p "$OUTPUT_DIR"

echo "Searching for library: $LIBRARY"
ID=$(context7 library "$LIBRARY" --json | jaq -r '.[0].id')

if [ -z "$ID" ]; then
  echo "Error: library '$LIBRARY' not found" >&2
  exit 1
fi

echo "Found: $ID"
echo "Fetching documentation..."

if [ -n "$QUERY" ]; then
  context7 docs "$ID" --query "$QUERY" --text > "$OUTPUT_DIR/${ID##/}.md"
else
  context7 docs "$ID" --text > "$OUTPUT_DIR/${ID##/}.md"
fi

echo "Saved to: $OUTPUT_DIR/${ID##/}.md"
```

**PowerShell script: key rotation check**

```powershell
$keys = context7 keys list 2>&1
if ($LASTEXITCODE -ne 0 -or $keys -match "No keys") {
    Write-Warning "No API keys configured. Run: context7 keys add ctx7sk-..."
    exit 1
}
Write-Host "Keys OK: $keys"
```

---

## Português

### Regras para agentes LLM usando context7-cli

Estas regras previnem erros comuns quando um agente LLM controla a CLI `context7`. Siga-as em todas as sessões.

#### Regra 1 — Sempre descubra IDs de bibliotecas com `library` primeiro

```bash
# CORRETO: descobrir o ID antes de buscar a documentação
context7 library react --json
# → usar o campo "id" retornado, ex: "/reactjs/react.dev"
context7 docs /reactjs/react.dev --query "useEffect"

# ERRADO: adivinhar o ID
context7 docs /react/react     # provavelmente 404
context7 docs react            # vai falhar
```

#### Regra 2 — Sempre use `--json` para saída legível por máquina

```bash
# CORRETO
context7 library react --json | jaq '.[0].id'

# EVITAR em scripting (cores ANSI quebram o parsing)
context7 library react
```

#### Regra 3 — Use `--text` ao inserir documentação no contexto de LLM

```bash
# CORRETO: texto plano, sem ANSI, limpo para janelas de contexto
context7 docs /reactjs/react.dev --query "hooks" --text

# EVITAR: JSON é verboso; use apenas quando precisar da estrutura de snippets
context7 docs /reactjs/react.dev --query "hooks" --json
```

#### Regra 4 — Nunca exponha chaves de API completas em prompts ou logs

```bash
# CORRETO: list mostra valores mascarados
context7 keys list
# Saída: 1. ctx7sk-abcd...xyz9

# EVITAR: export mostra valores completos — use apenas em ambientes seguros
context7 keys export
```

#### Regra 5 — Respeite os trust scores

- Faixa de trust score: 0–10
- Score ≥ 8: alta confiança — use diretamente
- Score 5–7: confiança média — verifique com o usuário antes de usar
- Score < 5: baixa confiança — sinalize, prefira outra fonte

#### Regra 6 — Trate erros sem quebrar o workflow

| Mensagem de erro | Causa | Ação do agente |
|------------------|-------|----------------|
| `Nenhuma chave de API encontrada` | Nenhuma chave configurada | Informar o usuário; pedir para executar `context7 keys add ctx7sk-...` |
| `401 Unauthorized` | Chave inválida | Pedir ao usuário que verifique e renove a chave em context7.com |
| `429 Too Many Requests` | Rate limit (após até 5 tentativas) | Aguardar 10–30 segundos; sugerir adicionar mais chaves |
| Erro de rede | Sem internet ou timeout | Reportar ao usuário; não fazer loop indefinido |
| `400 Bad Request` | Query malformada | Sanitizar a query e tentar novamente uma vez |

#### Regra 7 — Flags mutuamente exclusivas

`--text` e `--json` não podem ser combinadas. A CLI retornará erro se ambas forem passadas.

```bash
# ERRADO
context7 docs /reactjs/react.dev --text --json

# CORRETO
context7 docs /reactjs/react.dev --text
# ou
context7 docs /reactjs/react.dev --json
```

#### Regra 8 — Seleção de idioma (v0.2.0+)

Use `--lang` ou `CONTEXT7_LANG` para controlar o idioma da interface em pipelines multilíngues:

```bash
# Forçar inglês para parsing consistente em scripts
context7 --lang en library react --json

# Configurar permanentemente para uma sessão
export CONTEXT7_LANG=pt

# Saída em português para usuários brasileiros
context7 --lang pt docs /reactjs/react.dev --query "hooks" --text
```

Ordem de detecção: flag `--lang` → variável `CONTEXT7_LANG` → locale do sistema (qualquer locale `pt*` → português) → inglês (padrão).

---

### Templates de prompt

#### Template 1 — Descoberta de biblioteca

Use quando o usuário mencionar uma biblioteca pelo nome e você precisar encontrar o ID no Context7:

```
Preciso encontrar documentação para a biblioteca "<NOME_DA_BIBLIOTECA>".

Passo 1: execute este comando e faça o parsing do output JSON:
```bash
context7 library <NOME_DA_BIBLIOTECA> --json
```

Do array de resposta, selecione a entrada com o maior `trustScore`.
Apresente os 3 primeiros resultados ao usuário neste formato:
- `<id>`: <title> (trust score: <score>)

Pergunte ao usuário: "Sobre qual dessas bibliotecas você quer documentação?"

Após confirmar, prossiga para o Template 2 com o `id` selecionado.
```

#### Template 2 — Busca de documentação

Use após obter o ID da biblioteca pelo Template 1:

```
Vou agora buscar a documentação de `<LIBRARY_ID>` sobre: <TÓPICO>.

Execute:
```bash
context7 docs <LIBRARY_ID> --query "<TÓPICO>" --text
```

Insira o output completo no contexto da conversa.
Se o output tiver mais de 4000 tokens, resuma as seções mais relevantes.
Sempre cite o campo `codeId` (URL da fonte) fornecido no output.
```

#### Template 3 — Orientação para gerenciamento de chaves de API

Use quando o usuário precisar configurar ou solucionar problemas com chaves:

```
Para configurar as chaves de API do context7-cli, execute estes comandos:

1. Adicionar sua chave de API (obtenha uma em https://context7.com):
```bash
context7 keys add ctx7sk-SUA-CHAVE-AQUI
```

2. Verificar se foi salva:
```bash
context7 keys list
```

3. (Opcional) Adicionar mais chaves para melhor tolerância a rate limit:
```bash
context7 keys add ctx7sk-SEGUNDA-CHAVE
context7 keys add ctx7sk-TERCEIRA-CHAVE
```

4. Ver onde o arquivo de config está salvo:
```bash
context7 keys path
```

5. (Opcional) Verificar o status das chaves:
```bash
context7 keys list
# A rotação é automática — a CLI embaralha chaves por requisição
```
```

#### Template 4 — Comparação de múltiplas bibliotecas

Use quando o usuário quiser comparar documentação entre várias bibliotecas:

```
Vou buscar documentação de múltiplas bibliotecas para compará-las.

Para cada biblioteca na lista, execute:
```bash
context7 docs <id-da-biblioteca> --query "<TÓPICO>" --text
```

Apresente os resultados lado a lado com cabeçalhos:
## <Nome da Biblioteca> (<id-da-biblioteca>)
<conteúdo da documentação>

Após apresentar todos os resultados, resuma as principais diferenças.
```

#### Template 5 — Pipeline: buscar + buscar docs + resumir

Use para sessões automatizadas de pesquisa de documentação:

```
Sessão de pesquisa de documentação sobre: "<TÓPICO>"

Passo 1 — Encontrar bibliotecas relevantes:
```bash
context7 library <TÓPICO> --json
```
Selecione os 3 primeiros resultados por trustScore.

Passo 2 — Buscar docs de cada uma:
```bash
context7 docs <id1> --query "<TÓPICO>" --text
context7 docs <id2> --query "<TÓPICO>" --text
context7 docs <id3> --query "<TÓPICO>" --text
```

Passo 3 — Sintetizar: forneça um resumo único que cubra:
- O que cada biblioteca faz
- Como cada uma trata <TÓPICO>
- Principais diferenças e recomendações
```

---

### Integração com scripts shell

**Script Bash: buscar e salvar documentação**

```bash
#!/usr/bin/env bash
set -euo pipefail

BIBLIOTECA="${1:?Uso: $0 <nome-da-biblioteca> [query]}"
QUERY="${2:-}"
DIR_SAIDA="${3:-/tmp/context7-docs}"

mkdir -p "$DIR_SAIDA"

echo "Buscando biblioteca: $BIBLIOTECA"
ID=$(context7 library "$BIBLIOTECA" --json | jaq -r '.[0].id')

if [ -z "$ID" ]; then
  echo "Erro: biblioteca '$BIBLIOTECA' não encontrada" >&2
  exit 1
fi

echo "Encontrada: $ID"
echo "Buscando documentação..."

if [ -n "$QUERY" ]; then
  context7 docs "$ID" --query "$QUERY" --text > "$DIR_SAIDA/${ID##/}.md"
else
  context7 docs "$ID" --text > "$DIR_SAIDA/${ID##/}.md"
fi

echo "Salvo em: $DIR_SAIDA/${ID##/}.md"
```

**Script PowerShell: verificação de rotação de chaves**

```powershell
$chaves = context7 keys list 2>&1
if ($LASTEXITCODE -ne 0 -or $chaves -match "Nenhuma chave") {
    Write-Warning "Nenhuma chave de API configurada. Execute: context7 keys add ctx7sk-..."
    exit 1
}
Write-Host "Chaves OK: $chaves"
```