Claude integration

Ground Claude in your documents with CogniWeave.

Claude is powerful but offline relative to your corpus. CogniWeave retrieves the right passages with citations so Claude answers from your data, not its training set.

Why RAG with Claude

Claude's training has a cutoff and no knowledge of your private corpus. RAG injects fresh, cited passages at query time so answers stay grounded in your documents instead of the model's prior.

Integration flow

  • Index documents with CogniWeave (parse, chunk, embed, store).
  • Query CogniWeave for top-k passages relevant to the user's question.
  • Inject passages into the Claude prompt inside a <context> block.
  • Call Claude with a sensible max_tokens budget.
  • Parse the response and attach citations from the retrieval payload.

Node.js example

  • const { data: retrieved } = await cogniweave.retrieve(question);
  • const context = retrieved.map(p => `[#${p.rank}] ${p.text}`).join('\n');
  • const message = await claude.messages.create({
  • model: 'claude-3-5-sonnet-20241022',
  • max_tokens: 1024,
  • system: 'Answer using only the provided context. Cite passages by [#n].',
  • messages: [{ role: 'user', content: `Context:\n${context}\n\nQuestion: ${question}` }],
  • });

Context window strategy

  • Claude 3.5 Sonnet: 200k token window.
  • Retrieve top 5 passages (~2–3k tokens) for tight grounding.
  • Reserve ~5k tokens for the response.
  • Rerank before injection to drop low-relevance passages.

Streaming citations

Render inline markers like [#1], [#2] as links to the source document and page. CogniWeave returns `source_file`, `unit_id`, and `final_score` so the UI can resolve markers without a second round-trip.

FAQ

Common questions

How do I count tokens before sending to Claude?

Use Anthropic's tokenizer (`@anthropic-ai/tokenizer`) on the assembled prompt. Budget for system + context + question + expected response. Reserve at least 5k tokens for the answer.

What if a single retrieved passage is too long?

Re-chunk at retrieval time or truncate around the matched span. CogniWeave returns the chunk plus its parent, so you can downgrade to the smaller chunk when the parent overflows.

How do I avoid context overflow on multi-turn chats?

Summarize prior turns once they exceed ~30% of the window, and re-retrieve fresh context each turn instead of carrying it forward.

Can I stream the response while resolving citations?

Yes. Stream Claude's tokens to the client and resolve `[#1]`, `[#2]` markers against the retrieval payload (which you already have) as they appear.