Conversation
…ach chunk before embedding (codercops#554) ingest() takes contextualize: { provider }. For each chunk a model writes a sentence or two that places it in its page, with the page in the system prompt (so a provider that caches prompts reads it once) and the chunk in the message. The context is embedded with the chunk, the stored text stays the chunk and the context goes in its metadata. Turning it on or off, or changing the model, embeds pages again; a dry run asks no model; the stats count the chunks and the model tokens. chatcops ingest --contextualize turns it on from the config.
anurag629
approved these changes
Oct 31, 2026
anurag629
left a comment
Member
There was a problem hiding this comment.
Thanks @Saifjafri212213, this is careful work. With dev merged in, build, typecheck, lint (38), api:check, docs:check, coverage and all 85 e2e tests pass.
I also ran it for real with Claude over three docs pages:
- all 22 chunks got a context (the theming one: "This chunk is from the Theming section's 'Custom colors' subsection…"), and the stored text stayed the chunk alone
- a second run with the same setting embedded nothing
- turning it off re-embedded all three pages
Failing the page on a model error, so it's retried, is the right call over embedding it without context and never coming back to it.
Member
|
Merged, thanks @Saifjafri212213. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Contextual retrieval for
@chatcops/ingestandchatcops ingest. Before a chunk is embedded, a model writes a sentence or two that places it in its page, and that is embedded with the chunk, so "It costs $20 a month" still matches a question about the Team plan. It is the recipe Anthropic calls contextual retrieval.Closes #554
What changed
@chatcops/ingestIngestConfig.contextualize?: false | { provider: ProviderConfig | AIProvider }.src/contextualize.ts. For each chunk it asks the model one question: the whole page (doc.markdown, cut at 100,000 characters) is in the system prompt, and only the chunk (with its heading path) is in the message, withmaxTokens120. So the page is the same cached prefix for every chunk of a page. Chunks are asked one at a time and in order, so the first call can warm a provider's prompt cache and the rest read it. The system prompt says the page and the chunk are data, not instructions, since crawled text is untrusted. An answer is trimmed to one line and kept to 600 characters, and an empty answer means that chunk is embedded as it is.ingest.ts: the embedded text iscontext + "\n\n" + chunk.text. The stored record'stextstays the chunk alone, and the context is kept in the record's metadata ascontext. AProviderConfigis started withcreateProviderfrom@chatcops/core(already a peer dependency), loaded only when a run uses a model, and before anything is fetched, so a model that can't start fails the run withThe model for contextualize can't start: ...and not every page. A dry run starts no model and makes no call.contextualizefield withprovider:model(the config'stype:modelor the provider'sname:model). A page is reused only when that matches what the run is using, so turningcontextualizeon or off, or changing the model, embeds pages again. That check comes before the conditional request (If-None-Match), so a page the server would answer304for is fetched and embedded again as well.forcestill re-embeds everything.failed, with the reason): the page keeps what was indexed and its manifest record, so the next run tries it again. I chose that over embedding without context, which would be recorded as done and never retried.IngestStatshascontextualized(chunks that got a context) andmodelTokens({ inputTokens, outputTokens }, from the provider'sfinishusage, cache reads and writes included for Claude).@chatcops/cli--contextualize(in the help). The config holds which model, iningest.contextualize.provider, and the flag turns it on. Without the flag a run passescontextualize: false, so the CLI never spends on a model by surprise even when the config has a provider. With the flag and no provider it stops with exit 2 and--contextualize needs a model: set ingest.contextualize.provider in the config ..., before anything is embedded.Context written for N chunks by the model (X input, Y output tokens)., and--jsonhascontextualizedandmodelTokensinstatsnext toembedded.Docs and the rest: a "Contextual retrieval" section, the option row, the stats and the CLI line in
core/knowledge-base.mdx(with the cost note), minor changesets for both packages, andapi/ingest.api.md.Notes for review
scriptedProvider. The cost figure in the docs is Anthropic's (about $1 per million document tokens with caching), attributed to them, and the docs say a provider without prompt caching reads the page for every chunk.ingest()usescontextualizewhenever it is set, and only the CLI needs the flag. If you'd rather have the config alone turn it on in the CLI too, it is the one line that passesfalse.IngestStatsgained two required fields, so code that builds one by hand needs them (nothing in the repo does).How I Tested This
tests/contextualize.test.ts(ingest) failed before the change (the other two, a dry run that never starts a model and a long-answer cap, passed as guards), and all 7 new CLI tests failed too.scriptedProvider: the embedded text has the context in front; the stored text doesn't and the metadata has it; the page is in the system prompt, the same for every chunk, and the message has only the chunk; the data-not-instructions line; one call at a time and in order (checked with a provider that counts how many are running); the counts and the tokens in the stats; a dry run calls nothing and doesn't even start a provider that couldn't start; turning it on re-embeds unchanged pages that answer304, a second run with it embeds nothing, turning it off re-embeds again and clears the manifest field; changing the model re-embeds; the manifest records what wrote the context;forceasks again; a failing model fails the page and leaves the index and the manifest as they were; an empty answer embeds the chunk as it is; a long answer is capped; a long page is cut and says so; a provider config that can't start fails before anything is embedded;falseis off.--dry-run; turning the flag on re-embeds an unchanged page and a second run with it doesn't.undefined.dev),api:checkis up to date for both, andpnpm docs:checkfinds no errors in the knowledge base docs (other packages' samples fail it here for missing builds).AI assistance
An AI coding assistant helped write the change and the tests. I ran the checks listed above.