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, urgency, and refund flag."
5keywords:
6 - tanstack ai
7 - evaluate
8 - tutorial
9 - ticket
10 - typesafe
11 - jev
12 - start
13---
14
15You have support tickets and you need to route them in code.
16This tutorial builds a server route that returns a queue, an urgency, and a refund flag.
17
18## 1. Install
19
20<!-- ::start:tabs variant="package-manager" mode="install" -->
21
22react: @tanstack/ai @tanstack/ai-typesafe
23vue: @tanstack/ai @tanstack/ai-typesafe
24solid: @tanstack/ai @tanstack/ai-typesafe
25svelte: @tanstack/ai @tanstack/ai-typesafe
26preact: @tanstack/ai @tanstack/ai-typesafe
27angular: @tanstack/ai @tanstack/ai-typesafe
28vanilla: @tanstack/ai @tanstack/ai-typesafe
29octane: @tanstack/ai @tanstack/ai-typesafe
30
31<!-- ::end:tabs -->
32
33## 2. Set your key
34
35Get a key from [TypeSafe](https://typesafe.ai). Then set it in the environment:
36
37```bash
38TYPESAFE_API_KEY=your-typesafe-api-key
39```
40
41Keep the key on the server. Do not send it to the browser.
42
43## 3. Call `decide()` on the server
44
45Add a POST handler. Create the adapter once. Then ask three questions about the ticket.
46
47```typescript
48import { decide, choice, score, boolean } from '@tanstack/ai'
49import { typesafeDecider } from '@tanstack/ai-typesafe'
50
51type Ticket = {
52 subject: string
53 body: string
54}
55
56const ADAPTER = typesafeDecider('jev-latest')
57
58export async function POST(request: Request) {
59 const body = (await request.json()) as { ticket: Ticket }
60 const ticket = body.ticket
61
62 const result = await decide({
63 adapter: ADAPTER,
64 state: ticket,
65 questions: {
66 queue: choice({
67 instructions: 'Which team should handle this ticket?',
68 options: {
69 billing: 'Payments, invoices, refunds',
70 tech: 'Bugs, outages, integrations',
71 sales: 'Pricing, upgrades, new accounts',
72 },
73 }),
74 urgency: score({
75 instructions: 'How urgent is this ticket?',
76 levels: ['low', 'medium', 'high'],
77 }),
78 refund: boolean({
79 instructions: 'Is the customer asking for a refund?',
80 }),
81 },
82 })
83
84 return Response.json(result)
85}
86```
87
88In TanStack Start, put this in `src/routes/api.evaluate.ts`. Start maps that file to `/api/evaluate`.
89
90## 4. Read the answers
91
92Call the route. Then branch on the typed fields.
93
94```typescript
95type Ticket = {
96 subject: string
97 body: string
98}
99
100async function evaluateTicket(ticket: Ticket) {
101 const res = await fetch('/api/evaluate', {
102 method: 'POST',
103 headers: { 'Content-Type': 'application/json' },
104 body: JSON.stringify({ ticket }),
105 })
106 return res.json() as Promise<{
107 queue: { value: string; probability: number; confidence: number }
108 urgency: { value: string; probability: number; score: number }
109 refund: { value: boolean; probability: number }
110 }>
111}
112
113const result = await evaluateTicket({
114 subject: 'Charged twice for the same invoice',
115 body: 'Please refund the extra payment.',
116})
117
118if (result.refund.value) {
119 console.log('refund path', result.queue.value, result.urgency.value)
120} else {
121 console.log('route to', result.queue.value, result.urgency.value)
122}
123```
124
125You now have a queue, an urgency, and a refund flag. Try a POST with that ticket.
126
127## 5. Swap the adapter
128
129The `decide()` call stays the same. Only the adapter changes.
130
131- OpenRouter: `openRouterDecider('~typesafe/jev-latest')` and `OPENROUTER_API_KEY`
132- Vercel AI Gateway: `vercelGatewayDecider('typesafe-ai/jev')` and `AI_GATEWAY_API_KEY`
133- Cloudflare: `cloudflareDecider('typesafe/jev')` and a Worker binding, or `CLOUDFLARE_ACCOUNT_ID` plus `CLOUDFLARE_API_TOKEN`
134
135## 6. Try it
136
137Run the app. Paste a ticket. Pick a provider. Click Submit.
138
139The same app is on the Examples tab at `/ai/latest/docs/framework/react/examples/evaluate`.
140
141<!-- ::client-example library=ai framework=react slug=evaluate -->
142
143The full example is on GitHub: [TanStack/ai `examples/react/evaluate`](https://github.com/TanStack/ai/tree/main/examples/react/evaluate).
144
145See the [Evaluate guide](../evaluate/evaluate) for abort, middleware, and the full result shape.