Open History to see the last commit
1---
2title: Evaluate a ticket
3id: evaluate-tutorial
4description: "Build a TanStack Start route that asks typed questions about a support ticket and returns a queue, an urgency, and a refund flag your code can branch on."
5keywords:
6 - tanstack ai
7 - evaluate
8 - tutorial
9 - ticket
10 - typesafe
11 - jev
12 - start
13---
14
15This tutorial routes a support ticket with `decide()`. The result is a typed value the code can branch on.
16
17`decide()` asks typed questions about the ticket and gives back typed answers. In this tutorial, a server route returns a queue, an urgency, and a refund flag. It takes about 10 minutes.
18
19## 1. Install
20
21<!-- ::start:tabs variant="package-manager" mode="install" -->
22
23react: @tanstack/ai @tanstack/ai-typesafe
24vue: @tanstack/ai @tanstack/ai-typesafe
25solid: @tanstack/ai @tanstack/ai-typesafe
26svelte: @tanstack/ai @tanstack/ai-typesafe
27preact: @tanstack/ai @tanstack/ai-typesafe
28angular: @tanstack/ai @tanstack/ai-typesafe
29vanilla: @tanstack/ai @tanstack/ai-typesafe
30octane: @tanstack/ai @tanstack/ai-typesafe
31
32<!-- ::end:tabs -->
33
34## 2. Set your key
35
36Get a key from [TypeSafe](https://typesafe.ai). Set it in the server environment:
37
38```bash
39TYPESAFE_API_KEY=your-typesafe-api-key
40```
41
42Keep the key on the server. Do not send it to the browser.
43
44## 3. Ask the questions on the server
45
46Create `src/lib/evaluate-ticket.ts`. It asks three questions about the ticket. `POST` is a plain Web handler: it takes a `Request` and returns a `Response`.
47
48```typescript
49import { boolean, choice, decide, score } from '@tanstack/ai'
50import { typesafeDecider } from '@tanstack/ai-typesafe'
51
52type Ticket = {
53 subject: string
54 body: string
55}
56
57const adapter = typesafeDecider('jev-latest')
58
59export function evaluateTicket(ticket: Ticket) {
60 return decide({
61 adapter,
62 state: ticket,
63 questions: {
64 queue: choice({
65 instructions: 'Which team should handle this ticket?',
66 options: {
67 billing: 'Payments, invoices, refunds',
68 tech: 'Bugs, outages, integrations',
69 sales: 'Pricing, upgrades, new accounts',
70 },
71 }),
72 urgency: score({
73 instructions: 'How urgent is this ticket?',
74 levels: ['low', 'medium', 'high'],
75 }),
76 refund: boolean({
77 instructions: 'Is the customer asking for a refund?',
78 }),
79 },
80 })
81}
82
83export type TicketResult = Awaited<ReturnType<typeof evaluateTicket>>
84
85function readTicket(body: unknown): Ticket | null {
86 if (typeof body !== 'object' || body === null || !('ticket' in body)) {
87 return null
88 }
89 const ticket = body.ticket
90 if (typeof ticket !== 'object' || ticket === null) return null
91 if (!('subject' in ticket) || !('body' in ticket)) return null
92 if (typeof ticket.subject !== 'string' || typeof ticket.body !== 'string') {
93 return null
94 }
95 return { subject: ticket.subject, body: ticket.body }
96}
97
98export async function POST(request: Request) {
99 const ticket = readTicket(await request.json())
100 if (ticket === null) {
101 return new Response('Send { ticket: { subject, body } }.', {
102 status: 400,
103 })
104 }
105 return Response.json(await evaluateTicket(ticket))
106}
107```
108
109## 4. Mount the route
110
111Create `src/routes/api.evaluate.ts`. Start maps this file name to the `/api/evaluate` path.
112
113```typescript ignore
114import { createFileRoute } from '@tanstack/react-router'
115import { POST } from '@/lib/evaluate-ticket'
116
117export const Route = createFileRoute('/api/evaluate')({
118 server: {
119 handlers: {
120 POST: ({ request }) => POST(request),
121 },
122 },
123})
124```
125
126## 5. Branch on the answers
127
128Call the route from the client. `TicketResult` gives the answers their types, so `queue.value` is `'billing' | 'tech' | 'sales'`.
129
130```typescript ignore
131import type { TicketResult } from '@/lib/evaluate-ticket'
132
133async function postTicket(ticket: { subject: string; body: string }) {
134 const res = await fetch('/api/evaluate', {
135 method: 'POST',
136 headers: { 'Content-Type': 'application/json' },
137 body: JSON.stringify({ ticket }),
138 })
139 if (!res.ok) throw new Error(`Evaluate failed with ${res.status}`)
140 const result: TicketResult = await res.json()
141 return result
142}
143
144const result = await postTicket({
145 subject: 'Charged twice for the same invoice',
146 body: 'Please refund the extra payment.',
147})
148
149if (result.refund.value) {
150 console.log('refund path', result.queue.value, result.urgency.value)
151} else {
152 console.log('route to', result.queue.value, result.urgency.value)
153}
154```
155
156Send that ticket. You get a queue, an urgency, and a refund flag, and your code branches on them.
157
158## 6. Try it
159
1601. Run the app.
1612. Paste a ticket.
1623. Pick a provider and click Submit.
163
164The same app is on the Examples tab at `/ai/latest/docs/framework/react/examples/evaluate`.
165
166<!-- ::client-example library=ai framework=react slug=evaluate -->
167
168The full example is on GitHub: [TanStack/ai `examples/react/evaluate`](https://github.com/TanStack/ai/tree/main/examples/react/evaluate).
169
170## How it works
171
172`decide()` sends the ticket as `state` and asks every question about it. Each helper gives a different answer shape:
173
174- `choice`: `value` is one option key. You also get `probability` and `confidence`.
175- `score`: `value` is the nearest level label. `score` holds the raw number, and you also get `probability`.
176- `boolean`: `value` is `true` when the probability of yes is 0.5 or more. You also get `probability`.
177
178The result also has a `meta` field with the model and the usage.
179
180## Swap the provider
181
182The `decide()` call stays the same. Only the adapter changes:
183
184- **OpenRouter**: `openRouterDecider('~typesafe/jev-latest')` with `OPENROUTER_API_KEY`.
185- **Vercel AI Gateway**: `vercelGatewayDecider('typesafe-ai/jev')` with `AI_GATEWAY_API_KEY`.
186- **Cloudflare**: `cloudflareDecider('typesafe/jev')` with a Worker binding, or with `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`.
187
188For abort, middleware, and the full result shape, open the [Evaluate guide](../evaluate/evaluate).