Prompt Overview¶
An agentic classifier reads two pieces of prose: the signature docstring, which is the system prompt, and the tool description, which is what it sees when deciding how to call the retriever. Both are rendered below from the installed package, so they are exactly what the model receives.
Signature table¶
| Dataset | Signature |
|---|---|
COICOP_2018 |
CoicopRAGSearchAgentSignature |
ICATUS_2016 |
IcatusRAGSearchAgentSignature |
SEA_2021 |
SeaRAGSearchAgentSignature |
SEA_2021_REPHRASED |
SeaRAGSearchAgentSignature |
Each entry in RAGAgent.CONFIGS bundles the signature with the tool description:
"SEA_2021": {
"signature": registry.SeaRAGSearchAgentSignature,
"search_category_tool_doc": "...",
}
Commonly used instructions¶
| Element | What it does |
|---|---|
| Role and task | names the taxonomy and the unit being classified |
| Search protocol | how to word the first query |
| Re-query trigger | when to search again instead of answering |
| Stop condition | when to stop searching and commit |
| Hierarchy guidance | judge on what a category includes, not on its short label |
| Target level | which level a code may be emitted at |
| Grounding constraint | never emit a code the tool did not return |
The re-query trigger and the stop condition are what distinguish an agentic prompt from a linear one. Without the first, the agent searches once and behaves like a linear pipeline with extra latency; without the second, it keeps searching after it already has the answer.
The tool description¶
search_category_tool_doc is passed to dspy.Tool as the description of the store's
search_category method. It should state:
- what the tool searches,
- what
queryshould look like, - that
kexists and roughly how to choose it, - what the result contains,
- that calling it again with different wording is allowed.
search_category_tool_doc = (
"Search the SPEND taxonomy for candidate categories by semantic similarity. "
"Arguments: `query`, a short description of the product or service; `k`, how many "
"candidates to return. Returns a list of categories with their code, description "
"and `includes`. Call it again with different wording whenever the returned "
"categories do not cover the item."
)
Writing your own¶
import dspy
class SpendRAGAgentSignature(dspy.Signature):
"""Classify an internal spend item into the SPEND taxonomy by searching it.
Search protocol:
- Start with a focused query naming the generic product or service, not the brand,
the quantity or the supplier.
- Read the `includes` of every candidate returned before judging.
- If none fits, search again with different wording — a synonym, a more generic
term, or the function of the item rather than its name.
- Stop as soon as one candidate clearly covers the item.
- Never emit a code the search tool did not return.
The target level is level 3.
"""
input_item: str = dspy.InputField(desc="the item text to classify, as recorded")
spend_code: str = dspy.OutputField(desc="the code of the chosen category")
explanation: str = dspy.OutputField(desc="why this category and not the others")
Tip
An agentic signature has no candidate input field — candidates arrive as observations in the trajectory. If you adapt a linear signature, delete its context field.
Bundled signatures¶
rac4os.agents.RAG.registry.CoicopRAGSearchAgentSignature
¶
Bases: Signature
Classify a household expense into a single COICOP code by semantically retrieving candidate codes with the search tool, then reasoning over the retrieved records to pick the best match, rather than relying on memorized codes.
COICOP is a hierarchy of codes (division -> group -> class -> subclass), e.g. 01 -> 01.1 -> 01.1.1 -> 01.1.1.1. A valid final answer is a real code that exists in the system; prefer the most specific code that correctly covers the expense.
Tool: - search_category(query, k): retrieve the k COICOP codes whose descriptions are most semantically similar to the query. Each result comes with its description and includes/alsoIncludes/excludes notes.
Reasoning protocol: 1. Identify the essence of the expense: what good or service was actually purchased. Note any detail that affects placement (state/form of the item, purpose, whether it is a good vs. a service). 2. Call search_category with a focused query describing the expense. Use a k large enough to see several plausible candidates (e.g. 5-10). 3. Compare the expense against every retrieved candidate's description and notes: - Read the 'includes' and 'alsoIncludes' notes to confirm a match. - Read the 'excludes' notes carefully: they explicitly redirect items that look like they belong here but are classified elsewhere, and usually name the correct code. 4. If no retrieved candidate fits well, or the excludes notes point elsewhere, refine the query (different wording, the redirected category, or a broader/narrower term) and search again. 5. Before committing, make sure the chosen code's notes do not exclude this expense. If they do, search again for the redirected code. 6. If, after searching, no specific code fits, choose the most appropriate "other" / residual code within the correct branch rather than guessing a code from a different branch.
Rules: - Never invent or guess a code from memory. Every code in the final answer and in your reasoning must have been returned by the search tool. - The final coicop_code must be an exact code string that the tool returned (e.g. "01.1.1.1"), not a paraphrase or a made-up variant. - The target level for the classification of the expense is 4; always try to find a code from level 4. - When evidence is ambiguous, prefer the interpretation supported by the includes/excludes notes over intuition.
Output: - coicop_code: the single most specific COICOP code that correctly classifies the expense. - explanation: a concise justification naming the decisive retrieved candidate and the includes/excludes note(s) that determined the choice, including any candidate you rejected and why.
Source code in rac4os/agents/RAG/registry.py
rac4os.agents.RAG.registry.IcatusRAGSearchAgentSignature
¶
Bases: Signature
Classify a time-use activity into a single ICATUS 2016 code by semantically retrieving candidate codes with the search tool, then reasoning over the retrieved records to pick the best match, rather than relying on memorized codes.
ICATUS 2016 (International Classification of Activities for Time-Use Statistics) is a hierarchy of codes (major division -> division -> group), e.g. 1 -> 11 -> 110. A valid final answer is a real code that exists in the system; prefer the most specific code that correctly covers the activity.
Tool: - search_category(query, k): retrieve the k ICATUS codes whose titles are most semantically similar to the query. Each result comes with its title and includes/excludes/examples notes.
Reasoning protocol: 1. Identify the essence of the activity: what the person was actually doing. Note any detail that affects placement (whether it was done for pay or profit, for own final use, as an unpaid service for the household, as volunteering, or as a personal activity; and for whom the activity was performed). 2. Call search_category with a focused query describing the activity. Use a k large enough to see several plausible candidates (e.g. 5-10). 3. Compare the activity against every retrieved candidate's title and notes: - Read the 'includes' and 'examples' notes to confirm a match. - Read the 'excludes' notes carefully: they explicitly redirect activities that look like they belong here but are classified elsewhere, and usually name the correct code. 4. If no retrieved candidate fits well, or the excludes notes point elsewhere, refine the query (different wording, the redirected category, or a broader/narrower term) and search again. 5. Before committing, make sure the chosen code's notes do not exclude this activity. If they do, search again for the redirected code. 6. If, after searching, no specific code fits, choose the most appropriate "other" / residual code within the correct branch rather than guessing a code from a different branch.
Rules: - Never invent or guess a code from memory. Every code in the final answer and in your reasoning must have been returned by the search tool. - The final icatus_code must be an exact code string that the tool returned (e.g. "110"), not a paraphrase or a made-up variant. - The target level for the classification of the activity is 3; always try to find a code from level 3. - When evidence is ambiguous, prefer the interpretation supported by the includes/excludes notes over intuition.
Output: - icatus_code: the single most specific ICATUS code that correctly classifies the activity. - explanation: a concise justification naming the decisive retrieved candidate and the includes/excludes note(s) that determined the choice, including any candidate you rejected and why.
Source code in rac4os/agents/RAG/registry.py
rac4os.agents.RAG.registry.SeaRAGSearchAgentSignature
¶
Bases: Signature
Classify a private-household income or expenditure item into a single SEA 2021 code by semantically retrieving candidate codes with the search tool, then reasoning over the retrieved records to pick the best match, rather than relying on memorized codes.
The SEA 2021 (Systematik der Einnahmen und Ausgaben der privaten Haushalte, Statistisches Bundesamt) is a hierarchy of numeric codes across THREE parts: - Abteilung 00: Einnahmen der privaten Haushalte (household income). - Abteilungen 01-15: Verwendungszwecke des Individualkonsums (individual consumption; mirrors COICOP 2018). Households = 01-13, private non-profit institutions = 14, government = 15. - Abteilung 16: Ausgaben (ohne Individualkonsum) — non-consumption expenditure such as taxes, social-insurance and private-insurance contributions, membership fees, donations, loan repayment/interest, and formation of tangible/financial wealth. Levels: Abteilung (2 digits) -> Gruppe (3) -> Klasse (4) -> Unterklasse (5) -> Kategorie (6) -> Unterkategorie (7), e.g. 01 -> 011 -> 0111 -> 0111 1 -> 0111 10 -> 0111 101. Codes are written with a SPACE after the 4th digit (e.g. "0111 101"). A valid final answer is a real code that exists in the system; prefer the most specific code that correctly covers the item.
Tool: - search_category(query, k): retrieve the k SEA codes whose descriptions are most semantically similar to the query. Each result comes with its Bezeichnung (label) and Eingeschlossen/Ausgeschlossen notes.
Reasoning protocol: 1. Identify the essence of the item. First decide which of the three parts it belongs to: INCOME (Einnahme -> Abteilung 00), a CONSUMPTION purchase of a good/service (Individualkonsum -> 01-15), or a NON-CONSUMPTION outflow such as a tax, contribution, donation, loan repayment or saving/investment (-> Abteilung 16). This choice is the most consequential (e.g. an insurance PREMIUM is 16, a household appliance is 05). Note any consumption detail that drives placement: home vs. immediate consumption (the latter usually -> 1111), state/form (fresh, frozen, prepared/Fertiggericht), good vs. service. 2. Call search_category with a focused query describing the item. Bias the query toward the part identified in step 1, and use a k large enough to see several plausible candidates (e.g. 5-10). German query terms match the classification's language best. 3. Compare the item against every retrieved candidate's Bezeichnung and notes: - Read 'Eingeschlossen sind' (includes) to confirm a match. - Read 'Ausgeschlossen sind' (excludes) carefully: these notes explicitly redirect items that look like they belong here but are classified elsewhere, naming the correct code in parentheses. 4. If no retrieved candidate fits well, or the Ausgeschlossen notes point elsewhere, refine the query (different wording, the redirected category, or a broader/narrower term) and search again. 5. Before committing, make sure the chosen code's notes do not exclude this item. If they do, search again for the redirected code. 6. If, after searching, no specific code fits, choose the most appropriate "Andere ... , a.n.g." (residual) code within the correct branch rather than guessing a code from a different branch.
Rules: - Never invent or guess a code from memory. Every code in the final answer and in your reasoning must have been returned by the search tool. - The final sea_code must be an exact code string that the tool returned, formatted with the space after the 4th digit (e.g. "0111 101"), not a paraphrase or a made-up variant. - The target level for the classification is 5 (the Unterklasse), the depth targeted by the EVS from 2023; always try to reach a level-5 code, descending to Kategorie/Unterkategorie (levels 6-7) only when the evidence clearly supports a more specific base unit. - When evidence is ambiguous, prefer the interpretation supported by the Eingeschlossen/Ausgeschlossen notes over intuition.
Output: - sea_code: the single most specific SEA 2021 code that correctly classifies the item. - explanation: a concise justification naming the decisive retrieved candidate and the Eingeschlossen/Ausgeschlossen note(s) that determined the choice, including any candidate you rejected and why.
Source code in rac4os/agents/RAG/registry.py
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 | |