Skip to content

Usage

RAGChainOfThought · BaseRAGCoT

Linear RAG retrieves once and reasons once: the item is embedded, the k nearest categories are pulled from your retriever, and a chain-of-thought generation picks one of them. It is one retrieval call and one model call per item, which makes it the approach to try first.

See also

How It Works for what the pipeline does and where it breaks, Adapting Overview for running it on your own taxonomy.

Characteristics

Retrieval once, on the raw item
Model calls per item one
Needs a retriever yes, filled by you
k chosen by you, per call
Output your code field + explanation + reasoning + status

Running it

import dspy
from rac4os.classification_system import get_classification_system
from rac4os.rag.BaseRAG import BaseRAGCoT

cs = get_classification_system("SPEND_2026")    # or build one yourself

classifier = BaseRAGCoT(
    classification_system=cs,                   # the taxonomy object, not a name
    api_key="...",
    model_name="openai/gpt-4o-mini",
    api_base=None,                              # set for a self-hosted endpoint
    signature=SpendRAGCoTSignature,             # its docstring is the system prompt
    query_field="input_item",                   # which input field is the item
    context_field="retrieved_candidates",       # which input field is the candidate list
    store=store,                                # a filled Retriever
    temperature=0.0,                            # any further kwarg goes to dspy.LM
    max_tokens=2000,
)

prediction = classifier(query="two boxes of A4 paper for the printer", k=3)

prediction.spend_code
# '101010'

forward(query, k, where=None) performs the retrieval, serialises the candidates to JSON, and passes item and context to the generation under the two field names you declared. Both k and the optional metadata filter are yours to choose per call.

The classifier takes the classification system as an object rather than a name, so one loaded taxonomy can be shared between several classifiers instead of each parsing the source file again. RAGChainOfThought below still accepts a name and resolves it for you.

Every keyword argument beyond the ones listed above is collected as **generation_kwargs and forwarded to dspy.LM, which is how decoding is configured — temperature=0.0 for reproducible coding decisions, max_tokens large enough that the chain of thought and the answer both fit. See Generation settings.

With a registered configuration

If you would rather name the configuration once and reuse it, put it in CONFIGS and construct by name:

from rac4os.rag.RAG import RAGChainOfThought

RAGChainOfThought.CONFIGS["SPEND_2026"] = {
    "signature": SpendRAGCoTSignature,
    "query_field": "input_item",
    "context_field": "retrieved_candidates",
}

classifier = RAGChainOfThought(
    classification_name="SPEND_2026",
    api_key="...", model_name="openai/gpt-4o-mini", store=store,
)

Both forms are the same code path. RAGChainOfThought is BaseRAGCoT with a CONFIGS lookup in front of it.

Restricting the candidate space

Narrow retrieval to one branch or one level per call with a metadata filter:

prediction = classifier(
    query="docking station for the new laptops",
    k=5,
    where={"level": 3},
)

where is passed straight to the store and matches the metadata as your loader emitted it — if level left your loader as a string, filter with {"level": "3"}. Restricting the collection as a whole happens when you fill it, with store.index(codes_to_include=[...]).

Reading the prediction

prediction = classifier(query="two boxes of A4 paper for the printer", k=3)

prediction.spend_code
# '101010'

print(prediction.reasoning)
# The item is paper bought for use in a printer. Candidate 101010 "Printing and copying
# paper" lists copier paper and inkjet paper under includes, which is what A4 paper for a
# printer is. Candidate 101020 "Hygiene paper" covers towels and tissues, a different
# purpose despite also being paper. Candidate 102010 covers writing instruments.

print(prediction.explanation)
# Chosen 101010 because the item is copying paper, explicitly covered by that category's
# includes. 101020 was rejected because it covers hygiene paper, and 102010 because it
# covers writing instruments.

prediction.get_lm_usage()
# {'openai/gpt-4o-mini': {'prompt_tokens': 712, 'completion_tokens': 188, ...}}

reasoning comes from the chain-of-thought and is the model's working; explanation is a declared output field and is the justification you keep next to the code. In a production coding context the second one is what a human reviewer reads.

Note

The reasoning and explanation above are illustrative — the shape of what a generation returns, not a measured result. The retrieved candidates and all non-model output shown in these docs are real output from the example taxonomy.

Validating the predicted code

Nothing stops a model from answering with a code that is not in your classification — an invented one, a real code written in the wrong notation, or no code at all. The classifier checks the answer against the classification system after every call and records the outcome on the prediction as status:

prediction = classifier(query="two boxes of A4 paper for the printer", k=3)

prediction.status
# 'valid_code'
status what happened
valid_code the predicted code is a code of the classification system
invalid_code it is not — invented, wrongly formatted, or empty
no_validation the check was switched off with output_validation=False

The check is ClassificationSystem.is_valid_code, an exact lookup, so a code that is right but written differently — '101010 ' with a trailing space, or the dotted form of a taxonomy that writes its codes without separators — counts as invalid. That is deliberate: a code you cannot look up is a code you cannot use.

An invalid prediction is still returned. The classifier does not raise and does not substitute anything, so status has to be filtered on rather than caught:

rows = [{"item": item, "code": p.spend_code, "status": p.status} for item, p in results]
review = [r for r in rows if r["status"] != "valid_code"]

That is the honest end state for a coding pipeline: auto-code what validated, route the rest to a human. Pass output_validation=False to skip the lookup and get the model's raw answer untouched — status is then no_validation and nothing has been checked.

Classifying many items

A loop is the simplest usage, and dspy handles one call at a time:

rows = []
for item in items:
    p = classifier(query=item, k=5)
    rows.append({
        "item": item,
        "code": p.spend_code,
        "explanation": p.explanation,
        "usage": p.get_lm_usage(),
    })

Keep the usage counts: they are the only cheap signal on what a configuration costs before you run it at volume.

Because one item spends most of its wall time waiting on the provider, running several at once is almost always worth it. parallel_inference does that for you:

from rac4os.inference import parallel_inference

results = parallel_inference(
    classifier,
    [{"query": item, "k": 5, "where": {"level": 3}} for item in items],
    num_threads=5,
)

results[0].spend_code
# '101010'

Each dict is one call, and it has to carry every argument that classifier's forward takes — for linear RAG that is query and k, plus where if you filter. The classifier is constructed once, before it is handed over, and the same instance is shared across the threads.

num_threads is concurrency, not a chunk size: nothing is grouped into a single request, five threads simply means five calls in flight. Raise it until the provider starts rate-limiting you. Any further keyword argument is passed through to dspy.Parallel, which is what does the work — every classifier in rac4os is a dspy.Module, so you can also drive dspy.Parallel yourself if you want something this wrapper does not expose.

A failed item comes back as None, and ten of them end the run

The results list is index-aligned with your inputs, and an item that raised is None in it rather than missing — so results[i] always belongs to inputs[i], and [i for i, r in enumerate(results) if r is None] is what failed. The exceptions themselves are only logged, not raised.

Past dspy.Parallel's max_errors (10 by default) the run is cancelled and raises, discarding the results that already succeeded. On a long run that turns a handful of rate-limit errors into a total loss, so for anything substantial pass a ceiling you will not reach:

results = parallel_inference(
    classifier, inputs, num_threads=5, max_errors=len(inputs) + 1
)

Then retry just the failures, serially — which is what actually clears a 429:

retry = [inp for inp, r in zip(inputs, results) if r is None]
recovered = parallel_inference(classifier, retry, num_threads=1)

Common pitfalls

  • An empty candidate list almost always means an empty or wrongly named collection. BaseRAGCoT never indexes — see Retrievers.
  • The constructor takes no codes_to_include. Restrict the candidate space when you fill the store, with store.index(codes_to_include=[...]).
  • A missing input field raises immediately. forward checks the signature's input fields first, so a typo surfaces as ValueError: Missing input fields: ['input_item'] rather than as an obscure failure later.
  • forward here does not pin the LM. BaseRAGCoT relies on the dspy.configure call made in its constructor, so if you run several classifiers with different LMs in one process, construct each one immediately before use.