Add cost-aware routing and LLM budget tracking

- BudgetTracker tracks LLM token cost separately from tool cost
- LlmClient auto-records cost into request-scoped budget
- Router selects tier from classify confidence, intent risk, escalation count
- Supervisor short-circuits via regex fast-path when rule confidence is high
- Onboarding skill loads user escalation history to inform routing
This commit is contained in:
dahyeon committed 2026-05-19 01:28:54 -07:00
1 parent 211de18476
commit 21df14cd00
16 files changed
+473 -22

No files matched your search

+26
View File
@@ -1,6 +1,7 @@
import type { AgentRequest, AgentResponse, ConversationMessage } from '@agent/types';
import type { GarnetClient } from '@common/garnet';
import type { NatsClient } from '@common/nats';
import type { EscalationHistoryService } from '../../memory/escalation-history.js';
import type { MemoryService } from '../../memory/memory-types.js';
import {
buildLongTermSummary,
@@ -118,6 +119,7 @@ export class OnboardingSkill implements Skill {
shadowEvalPublisher?: ShadowEvalPublisher,
private readonly natsClient?: Pick<NatsClient, 'request'>,
private readonly memoryService?: MemoryService,
private readonly escalationHistoryService?: EscalationHistoryService,
) {
this.classifier = new OnboardingIntentClassifier(llmClient);
this.blackboardStore = new OnboardingBlackboardStore(
@@ -186,13 +188,17 @@ export class OnboardingSkill implements Skill {
const deriveStep = trace?.startStep('derive');
const derived = deriveState(session, request.trigger, classified.intent);
const escalationHistory = await this.loadEscalationHistory(request.userId);
const routerMode = getAgentRouterMode();
if (routerMode === 'static' || routerMode === 'learned-v1') {
const routerInput = {
classified,
derived,
intentRiskTier: this.capabilityCard().riskTier,
recentLatencyP95Ms: this.getRecentLatencyP95Ms(request),
request,
session,
userEscalationCount: escalationHistory?.totalEscalations ?? 0,
};
derived.routingDecision =
routerMode === 'learned-v1'
@@ -353,6 +359,26 @@ export class OnboardingSkill implements Skill {
return normalizeOnboardingSession(rawSession);
}
private async loadEscalationHistory(userId: string) {
if (!this.escalationHistoryService) {
return null;
}
try {
return await this.escalationHistoryService.load(userId);
} catch (error) {
getRequestLogger().warn(
{ err: error },
'[OnboardingSkill] Failed to load escalation history',
);
return null;
}
}
private getRecentLatencyP95Ms(request: AgentRequest): number | undefined {
const raw = request.context?.['recentLatencyP95Ms'];
return typeof raw === 'number' && Number.isFinite(raw) ? raw : undefined;
}
private async decideEarlyAbstention(
request: AgentRequest,
classified: ClassifiedIntent,