# Cookbook

> Runnable recipes in curl, Python and TypeScript for the ten Dex use cases, each with the answer the live API gave, plus patterns for routing, agent gates, batches, retries, version pins and cost.

Each recipe is a complete program you can run with a free test key. The use-case recipes show the request, the same call in curl, Python and TypeScript, the response the live API returned for it, and the code that acts on the answer. The patterns show how to build Dex into a larger system.

## Use cases

| Recipe | Questions | What your code does with the answer |
| --- | --- | --- |
| [Route and prioritise support tickets](/docs/cookbook/support-routing/) | pick, rate, check | Routes the ticket to a team, raises the priority, flags churn risk |
| [Moderate marketplace posts](/docs/cookbook/moderation/) | pick, rate, check | Removes clear scams, queues the rest for reviewers |
| [Score inbound leads](/docs/cookbook/lead-scoring/) | pick, rate, check | Fills CRM fields and books a call |
| [Gate an agent's tool call](/docs/cookbook/agent-guardrails/) | pick, check, check | Runs, confirms or blocks the call |
| [Triage documents and email](/docs/cookbook/document-triage/) | pick, pick, check, rate | Files the email with the right department |
| [Categorise product listings](/docs/cookbook/categorisation/) | pick, check, pick | Sets the category and condition, holds prohibited items |
| [Read sentiment and stance](/docs/cookbook/sentiment/) | rate, pick, check | Feeds a dashboard, alerts on price complaints |
| [Check messages against a policy](/docs/cookbook/compliance/) | check, check, pick, rate | Stops a message before it is sent |
| [Grade LLM outputs](/docs/cookbook/llm-grading/) | check, check, check, rate | Scores an eval run on a pinned version |
| [Check data quality](/docs/cookbook/data-quality/) | check, pick, check | Merges duplicates, fixes field types |

## Patterns

- [Route between an LLM and Dex](/docs/cookbook/route-llm-and-dex/): Dex decides every item; an LLM writes text only for the items that need it.
- [Gate every tool call of an agent](/docs/cookbook/gate-agent-tool-calls/): one function in front of your tools that allows, asks or blocks.
- [Score a batch](/docs/cookbook/batch-scoring/): thousands of items with bounded concurrency, safe retries and a results file.
- [Retries, timeouts and fallbacks](/docs/cookbook/retries-and-fallbacks/): what the SDKs already do, and what to write yourself with plain HTTP.
- [Pin versions in CI](/docs/cookbook/pin-versions-in-ci/): tests that give the same answer every run, and a check that warns before a version retires.
- [Estimate cost](/docs/cookbook/cost-estimation/): exact token counts from a free test call, and which plan covers your volume.

## How the recipes were made

- Every request file validates against the [API contract](/openapi.yaml), and a test in our build checks each response against its request: answers in question order, probabilities that sum to 1, the choice with the highest probability, and `abstained` exactly when the confidence is below `min_confidence`.
- The responses were captured from `https://api.thinqit.ai` with a test key. They name the model version that answered. On that exact version the same request always returns the same answers, so you get the output shown when you send the same request.
- The requests use the alias `dex-1` unless they pin a version. When the alias moves to a newer version, your answers can differ slightly from the ones shown. Pin the version named in the response to reproduce them exactly (see [Determinism](/docs/concepts/determinism/)).

## Before you run them

1. Create a test key in the [console](/console/) and put it in `DEX_API_KEY`. Test keys are free up to 250,000 tokens a day per account; every recipe uses a few hundred.
2. Install the SDKs from [Downloads](/docs/reference/sdks/#downloads) (Python 3.10 or newer, Node 18 or newer).
3. Save Python code as a `.py` file and run it with `python`. Save TypeScript code as a `.mts` file and run it with `npx tsx`, for example `npx tsx support-routing.mts`: the samples use `await` at the top level, which needs an ES module, and the `.mts` ending makes the file one whatever your `package.json` says.

To implement one of these recipes with an AI coding assistant, give it the rules on [Implement Dex with an AI assistant](/docs/ai-assistants/).
