Skip to content

Evaluate

decide() returns a typed value your code can branch on, such as a queue name, an urgency level, or a yes or no.

decide() asks a decision model typed questions about your data. You read the answers as typed fields, like result.queue.value. There is no stream.

Quick setup

  1. Install the core package and the TypeSafe adapter:
shell
npm i @tanstack/ai @tanstack/ai-typesafe
  1. Put your key on the server. The adapter reads it at runtime:
shell
TYPESAFE_API_KEY=your-typesafe-api-key
  1. Call decide() on the server with a state and a map of questions:
ts
import { decide, choice, score, boolean } from '@tanstack/ai'
import { typesafeDecider } from '@tanstack/ai-typesafe'

const ticket = {
  subject: 'Charged twice for the same invoice',
  body: 'Please refund the extra payment.',
}

const result = await decide({
  adapter: typesafeDecider('jev-latest'),
  state: ticket,
  questions: {
    queue: choice({
      instructions: 'Which team should handle this ticket?',
      options: {
        billing: 'Payments, invoices, refunds',
        tech: 'Bugs, outages, integrations',
        sales: 'Pricing, upgrades, new accounts',
      },
    }),
    urgency: score({
      instructions: 'How urgent is this ticket?',
      levels: ['low', 'medium', 'high'],
    }),
    refund: boolean({
      instructions: 'Is the customer asking for a refund?',
    }),
  },
})

if (result.queue.value === 'billing' && result.refund.value) {
  console.log('Send to billing as a refund, urgency:', result.urgency.value)
}

result.queue.value has the type 'billing' | 'tech' | 'sales'. TypeScript knows every answer.

How it works

  • state is the data that every question judges. It can be a string, an object, or an array. An array is one state, not a batch.
  • Each key in questions becomes a property on the result.
  • The key meta is reserved. result.meta holds the model and the token usage.
  • Every evaluate adapter uses the same decide() call. To change provider, change the adapter.

The three question types

choice()

The model picks one key from options. The option keys become the union type of .value. If a key needs no description, use null.

ts
import { choice } from '@tanstack/ai'

const queue = choice({
  instructions: 'Which team should handle this ticket?',
  options: {
    billing: 'Payments, invoices, refunds',
    tech: 'Bugs, outages, integrations',
    sales: 'Pricing, upgrades, new accounts',
  },
})

score()

The model rates state on ordered levels, lowest first. You must pass at least two levels. .value is the nearest level label. .score is the raw fraction.

ts
import { score } from '@tanstack/ai'

const urgency = score({
  instructions: 'How urgent is this ticket?',
  levels: ['low', 'medium', 'high'],
})

boolean()

A yes or no question. If the probability of true is 0.5 or more, .value is true. There is no .confidence.

ts
import { boolean } from '@tanstack/ai'

const refund = boolean({
  instructions: 'Is the customer asking for a refund?',
  criteria: {
    true: 'The customer asks for money back',
    false: 'The customer asks for something else',
  },
})

criteria is optional. Use it to describe each side.

Pick a provider

ProviderAdapterKey
OllayaollayaDecider('laya:latest') (@tanstack/ai-ollaya)No key. Local server at http://127.0.0.1:11435.
TypeSafetypesafeDecider('jev-latest') (@tanstack/ai-typesafe)TYPESAFE_API_KEY
OpenRouteropenRouterDecider('~typesafe/jev-latest') (@tanstack/ai-openrouter)OPENROUTER_API_KEY
Vercel AI GatewayvercelGatewayDecider('typesafe-ai/jev') (@tanstack/ai-vercel-gateway)AI_GATEWAY_API_KEY (or VERCEL_OIDC_TOKEN)
CloudflarecloudflareDecider('typesafe/jev') (@tanstack/ai-cloudflare)A Worker binding, or CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN
OpenAIopenaiDecider('gpt-6-luna') (@tanstack/ai-openai)OPENAI_API_KEY

You only need the key for the adapter you pick. To use OpenRouter, install @tanstack/ai-openrouter and swap the adapter:

ts
import { decide, boolean } from '@tanstack/ai'
import { openRouterDecider } from '@tanstack/ai-openrouter'

const result = await decide({
  adapter: openRouterDecider('~typesafe/jev-latest'),
  state: 'Please refund the extra payment.',
  questions: {
    refund: boolean({
      instructions: 'Is the customer asking for a refund?',
    }),
  },
})

console.log(result.refund.value)

Call it from the browser

decide() needs your API key, so it runs on the server. Put it in a route and call the route from the client.

Server route:

ts
import { decide, choice, score, boolean } from '@tanstack/ai'
import { typesafeDecider } from '@tanstack/ai-typesafe'

export async function POST(request: Request) {
  const body: unknown = await request.json()
  if (
    typeof body !== 'object' ||
    body === null ||
    !('ticket' in body) ||
    typeof body.ticket !== 'object' ||
    body.ticket === null
  ) {
    return new Response('Invalid request body', { status: 400 })
  }

  const result = await decide({
    adapter: typesafeDecider('jev-latest'),
    state: body.ticket,
    questions: {
      queue: choice({
        instructions: 'Which team should handle this ticket?',
        options: {
          billing: 'Payments, invoices, refunds',
          tech: 'Bugs, outages, integrations',
          sales: 'Pricing, upgrades, new accounts',
        },
      }),
      urgency: score({
        instructions: 'How urgent is this ticket?',
        levels: ['low', 'medium', 'high'],
      }),
      refund: boolean({
        instructions: 'Is the customer asking for a refund?',
      }),
    },
  })

  return Response.json(result)
}

Client:

ts
export async function evaluateTicket(ticket: { subject: string; body: string }) {
  const res = await fetch('/api/evaluate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ ticket }),
  })
  return res.json()
}

For a full Start route, see the tutorial Evaluate a ticket.

Try the example

examples/react/evaluate is a small TanStack Start app. Paste a support ticket and the UI shows queue, urgency, and refund. A dropdown lists five adapters, and every one runs the same questions.

  1. Copy .env.example to .env.
  2. If you pick a hosted adapter, add its key in .env.
  3. If you pick Ollaya, run ollaya serve, then ollaya pull laya:latest. Ollaya needs no API key.
  4. Run the dev server:
shell
pnpm --filter evaluate dev

Open http://localhost:3100 and paste a ticket.

Advanced

Result fields

ts
import { decide, choice, score, boolean } from '@tanstack/ai'
import { typesafeDecider } from '@tanstack/ai-typesafe'

const result = await decide({
  adapter: typesafeDecider('jev-latest'),
  state: 'Please refund the extra payment.',
  questions: {
    queue: choice({
      instructions: 'Which team should handle this ticket?',
      options: {
        billing: 'Payments, invoices, refunds',
        tech: 'Bugs, outages, integrations',
      },
    }),
    urgency: score({
      instructions: 'How urgent is this ticket?',
      levels: ['low', 'medium', 'high'],
    }),
    refund: boolean({
      instructions: 'Is the customer asking for a refund?',
    }),
  },
})

console.log(result.queue.value)
console.log(result.queue.probability)
console.log(result.queue.confidence)
console.log(result.queue.probabilities)

console.log(result.urgency.value)
console.log(result.urgency.score)
console.log(result.urgency.probability)
console.log(result.urgency.confidence)
console.log(result.urgency.legend)
console.log(result.urgency.probabilities)

console.log(result.refund.value)
console.log(result.refund.probability)

console.log(result.meta.model)
console.log(result.meta.usage)
QuestionFields
choice.value is the selected option key. .probability is P(selected). .probabilities is the full map. .confidence is also present.
score.value is the nearest level. .score is the raw fraction. .probability is P(that level). .legend maps each level index to its label. .probabilities is the full map, keyed by level index.
boolean.value is true when P(true) is 0.5 or more. .probability is P(true). No .confidence.

On the TypeSafe wire, the boolean question type is named noul.

decide() options

OptionTypeDescription
adapterEvaluateAdapterAn evaluate adapter with a model. Required.
statestring | object | arrayThe data that every question judges. Required.
questionsRecord<string, Question>Map of choice, score, and boolean questions. Required.
abortSignalAbortSignalCancel the request
timeoutnumberStop the request after this many milliseconds
modelOptionsprovider optionsProvider-specific options
middlewareArray<GenerationMiddleware>Observe-only lifecycle hooks (usage, finish, error, abort)
debugDebugOptionDebug logging

instructions on each question can also be a string, an object, or an array.

API keys in code

typesafeDecider reads TYPESAFE_API_KEY. To pass a key in code, use createTypesafeDecider('jev-latest', 'ts-...').

Cancel a request

Pass an abortSignal. To stop after a fixed time, pass timeout in milliseconds instead. A timeout rejects with a TimeoutError.

ts
import { decide, boolean } from '@tanstack/ai'
import { typesafeDecider } from '@tanstack/ai-typesafe'

const controller = new AbortController()
setTimeout(() => controller.abort(), 5000)

const result = await decide({
  adapter: typesafeDecider('jev-latest'),
  state: 'Please refund the extra payment.',
  questions: {
    refund: boolean({
      instructions: 'Is the customer asking for a refund?',
    }),
  },
  abortSignal: controller.signal,
})

console.log(result.refund.value)

Track usage

Add observe-only middleware to track usage, completion, errors, and cancellation. It is the same GenerationMiddleware contract that the media activities use. See Middleware.

ts
import { decide, boolean } from '@tanstack/ai'
import { typesafeDecider } from '@tanstack/ai-typesafe'

const result = await decide({
  adapter: typesafeDecider('jev-latest'),
  state: 'Please refund the extra payment.',
  questions: {
    refund: boolean({
      instructions: 'Is the customer asking for a refund?',
    }),
  },
  middleware: [
    {
      name: 'usage-logger',
      onUsage: (_ctx, usage) => {
        console.log(`prompt tokens: ${usage.promptTokens}`)
      },
    },
  ],
})

console.log(result.refund.value)

Tip

Pass otelMiddleware() to emit OpenTelemetry spans for evaluate calls. See OpenTelemetry.

Errors

decide() throws in these cases:

  • questions is empty. It throws before any request.
  • A question key is meta. It throws before any request.
  • The provider answers, but decide() cannot map an answer. For example, an answer is missing, has the wrong type, or has no probability for its value.

In the last case, onUsage and the evaluate:usage event still get the token usage of that call, before onError runs. onFinish does not run.

ts
import { decide, boolean } from '@tanstack/ai'
import { typesafeDecider } from '@tanstack/ai-typesafe'

try {
  const result = await decide({
    adapter: typesafeDecider('jev-latest'),
    state: 'Please refund the extra payment.',
    questions: {
      refund: boolean({
        instructions: 'Is the customer asking for a refund?',
      }),
    },
  })
  console.log(result.refund.value)
} catch (error) {
  if (error instanceof Error) {
    console.error('Evaluate failed:', error.message)
  }
}

Refused questions

OpenAI can refuse to answer a question. Then decide() throws an EvaluateError with code: EVALUATE_REFUSAL_CODE. The error keeps what the provider sent back:

  • usage: the tokens the call used.
  • model: the model that answered.
  • answers: the raw answers that came back, before decide() maps them. The refused question has no key.

The raw answers do not have value. A choice() answer has choice, for example { type: 'choice', choice: 'billing', ... }. A boolean() answer is { type: 'noul', noul: 0.9 }, where noul is the chance of true.

Use isEvaluateError(error) to narrow a caught error. It checks name and code, so it works even when the app has two copies of @tanstack/ai. instanceof EvaluateError does not.

ts
import {
  decide,
  boolean,
  choice,
  isEvaluateError,
  EVALUATE_REFUSAL_CODE,
} from '@tanstack/ai'
import { openaiDecider } from '@tanstack/ai-openai'

try {
  const result = await decide({
    adapter: openaiDecider('gpt-6-luna'),
    state: 'Please refund the extra payment.',
    questions: {
      queue: choice({
        instructions: 'Which team should handle this ticket?',
        options: {
          billing: 'Payments, invoices, refunds',
          tech: 'Bugs, outages, integrations',
        },
      }),
      refund: boolean({
        instructions: 'Is the customer asking for a refund?',
      }),
    },
  })
  console.log(result.refund.value)
} catch (error) {
  if (isEvaluateError(error) && error.code === EVALUATE_REFUSAL_CODE) {
    console.error(error.message) // OpenAI declined to answer question "refund"
    console.log(error.usage)
    console.log(error.answers?.queue) // { type: 'choice', choice: 'billing', ... }
  }
  throw error
}

The provider still bills a refused call. So before decide() rethrows an EvaluateError that has usage, it reports that usage:

  1. Middleware onUsage runs with the usage.
  2. Middleware onError runs with the error.
  3. decide() rethrows the same error.

onFinish does not run. See Track usage for middleware.