Skip to main content

Class: QueryClassifier

Defined in: packages/agentos/src/query-router/QueryClassifier.ts:165

Chain-of-thought LLM classifier that determines retrieval depth (T0-T3) for each incoming query.

Example

const classifier = new QueryClassifier({
model: 'gpt-4o-mini',
provider: 'openai',
confidenceThreshold: 0.7,
maxTier: 3,
topicList: 'Auth (docs/auth.md)\nDB (docs/db.md)',
toolList: 'search_code, read_file',
});

const result = await classifier.classify('How does auth work?');
console.log(result.tier); // 1

Constructors

Constructor

new QueryClassifier(config): QueryClassifier

Defined in: packages/agentos/src/query-router/QueryClassifier.ts:173

Creates a new QueryClassifier instance.

Parameters

config

QueryClassifierConfig

Classifier configuration with model, provider, and thresholds.

Returns

QueryClassifier

Methods

classify()

classify(query, conversationHistory?): Promise<ClassificationResult>

Defined in: packages/agentos/src/query-router/QueryClassifier.ts:193

Classifies a user query into a retrieval tier.

Steps:

  1. Builds a chain-of-thought system prompt with tier definitions, topic list, tool list, and optional conversation context.
  2. Calls the LLM via generateText.
  3. Parses the JSON response (handling optional markdown code fences).
  4. Applies confidence-based tier bumping: if confidence < threshold, tier += 1.
  5. Caps the tier at the configured maxTier.
  6. On ANY error, returns a safe T1 fallback with confidence 0.

Parameters

query

string

The user's query text to classify.

conversationHistory?

ConversationMessage[]

Optional recent conversation messages for context.

Returns

Promise<ClassificationResult>

A ClassificationResult with tier, confidence, reasoning, and metadata.