Yolk SDK
Agent

Classifier models

Ask typed questions about data and get calibrated probabilities instead of text.

Classifier models such as TypeSafe's Jev answer typed questions about one value with probabilities. They do not chat or generate text. Use them for fast, typed decisions: route a ticket, score urgency, or check whether a reply approves a result.

AI Gateway calls this capability evaluation (POST /v1/evaluate). Yolk calls it classification everywhere (classify, ClassifierModel, ClassificationResult); the Gateway provider maps the wire name.

Install:

pnpm add @yolk-sdk/agent@canary effect@4.2.0

Subpaths

SubpathUse for
@yolk-sdk/agent/classificationprovider-neutral questions, answers, usage, errors, and classify
@yolk-sdk/agent/providers/vercel/ai-gateway-classifierClassifierModel layer over AI Gateway POST /v1/evaluate
@yolk-sdk/agent/providers/vercel/conformanceclassifier conformance cases and synthetic fixtures for replay tests
@yolk-sdk/emulators/gatewayfixture-only /v1/evaluate route next to the Gateway chat emulator route

Classify

A classification is one state (a string, JSON object, or JSON array; an array is one state, not a batch) and named questions answered together. This fragment routes a ticket; gatewayKey is your host-owned AI Gateway API key or Vercel OIDC token.

import { Effect, Redacted } from 'effect'
import { FetchHttpClient } from 'effect/http'
import { classify } from '@yolk-sdk/agent/classification'
import { makeVercelAiGatewayClassifierLayer } from '@yolk-sdk/agent/providers/vercel/ai-gateway-classifier'

const program = classify({
  state: { subject: 'Invoice question', body: 'I was charged twice.' },
  questions: {
    route: {
      type: 'choice',
      instructions: 'Which team should handle this ticket?',
      criteria: {
        billing: 'Payments and refunds.',
        bug: 'Product defects.',
        other: 'Anything else.'
      }
    },
    urgent: { type: 'boolean', instructions: 'Does the customer need an answer today?' }
  }
}).pipe(
  Effect.map(result => ({
    team: result.answers.route.choice, // 'billing' | 'bug' | 'other'
    urgent: result.answers.urgent.probability > 0.7,
    costUsd: result.usage?.costUsd
  })),
  Effect.provide(makeVercelAiGatewayClassifierLayer({ apiKey: Redacted.make(gatewayKey) })),
  Effect.provide(FetchHttpClient.layer)
)

classify types each answer from its question: a choice answer's choice is the union of that question's option keys. VercelAiGatewayClassifierLayer reads the same credential as the Gateway chat provider from the environment (AI_GATEWAY_API_KEY, falling back to VERCEL_OIDC_TOKEN) and provides the Fetch HttpClient.

Questions and answers

QuestioncriteriaAnswer
booleanoptional { true, false } descriptions{ type: 'boolean', probability } (probability of true)
choiceoption key to a description or JSON value (2 to 255 options){ type: 'choice', choice, probabilities, confidence? }
scoreordinal levels, lowest first, as descriptions or JSON (2–10 levels){ type: 'score', score, probabilities, confidence? }

Probabilities are kept exactly as the provider returns them (Jev rounds to two decimals) and are never renormalized. Every answer is checked against its question: a missing answer, an answer for a question that was not asked, a type mismatch, a choice that names no option, or a probability or confidence that is not a finite number in [0, 1] fails the classification.

ClassificationResult carries model, answers (keyed by question id), usage (inputTokens, outputTokens, and costUsd when the provider reports a cost), and providerMetadata.

Errors

Failures are typed errors in the Effect error channel, never result flags:

ErrorWhen
ClassificationRequestInvalidthe request breaks the contract (for example 256 options); nothing is sent
ClassificationProviderErrorthe provider is unreachable or rejects the request; provider has the kind, status, and error code
ClassificationResponseInvalidthe response or an answer does not fit the request; usage is kept because the request was billed

Provider error messages are status-only (Vercel AI Gateway returned 404); they never carry the provider's body or your credential.

AI Gateway

  • Calls POST https://ai-gateway.vercel.sh/v1/evaluate with typesafe-ai/jev by default. Set model to choose another classifier model, and baseUrl only for a trusted proxy or a local emulator: the bearer credential is sent there.
  • providerOptions passes through unchanged, so Gateway options such as { gateway: { zeroDataRetention: true, only: [...], models: [...] } } reach the Gateway as you write them.
  • providerMetadata.gateway.cost (US dollars) becomes usage.costUsd.
  • Evaluation fallback response headers (x-ai-gateway-evaluation-fallback-*) are kept in providerMetadata.evaluationFallbackHeaders.
  • choice and score confidence is read from the answer, or else from providerMetadata.typesafe.confidence[questionId]. The live Gateway sends both.
  • Score levels are indexed from 0: probabilities keys run from "0" to "n-1" and score is the probability-weighted level index.
  • 408, 429, 5xx, and transport failures are retried up to maxRetries times (default 2; 0 disables) with exponential backoff and full jitter (base 250 ms, cap 4 s), or after the response's Retry-After (seconds or an HTTP date, capped at 10 s). Other 4xx statuses and responses that fail to decode are never retried. Delays use the Effect Clock, so TestClock drives them in tests; interrupting the classification stops the retries. After the last attempt you get the same typed error a single attempt would give.

Classify from code mode scripts

makeClassifierTool from @yolk-sdk/codemode exposes a ClassifierModel as a code-mode-only classify tool, so one script can classify many items with per-script and per-process concurrency caps. See Add code mode.

Policy stays in your host

Classification is a read with no side effects. A classifier can say a command looks destructive, but your host policy decides whether it runs. Thresholds, model choice, credentials, and what to do with each answer are host-owned.

Testing

The four classifier conformance cases (vercelAiGatewayClassifierConformanceCases: boolean, choice, score, and the error envelope) need only an HttpClient and VercelAiGatewayClassifierConformanceConfig. Replay them from vercelAiGatewayClassifierConformanceFixtures with ReplayHttpClient, or run them against the @yolk-sdk/emulators/gateway emulator, whose emulator.evaluate part answers the same synthetic recordings. The fixtures are synthetic placeholders (evidence: 'unverified') until an owner-approved live probe records them.

On this page