Skip to main content
A customer writes “i want assistance with my utility bill”. They could want to pay it, or to see what they owe. A bank’s assistant has to give each request a route: the one thing the app should do with it. Most requests are clear: “what’s my balance” has one route. An app that guesses on the unclear ones does the wrong thing some of the time, and an app that asks about every request is slow to use. Here we build a banking assistant as a Pydantic AI agent. Jev picks the route and returns a probability for every route. Jev is the agent’s model, through Pydantic AI’s TypeSafeModel. When the probability of Jev’s pick is 0.8 or more, the app acts on it. When it is lower, Pydantic AI raises UnsureRoute, and the app asks the customer to choose among the routes to which Jev gave a probability of 0.1 or more. On 600 labeled requests from CLINC150 (Larson et al., CC BY 3.0), the app asked the customer on 35, about 1 in 17. Jev’s pick matched the label on 20 of those 35, and the routes the app offered included the label’s route on 34. A request “ends correctly” if the app acts on the label’s route, or asks and offers it. You end up with respond(), which takes one customer request and returns what the app does next: the route to act on, or the routes to offer.

Setup

Run in Colab

Runs as published without an API key.

Python file

This page as one .py file, to run locally.

Open in playground

Edit one request and run it live. Needs a TypeSafe account.
Requires Python 3.11 or later.
With uv, uv run pydantic_ai_banking_assistant.py installs the dependencies for you. The code replays saved answers, so it runs as published without an API key. To change a request, set TYPESAFE_API_KEY and OPENAI_API_KEY; in Colab, add them in the Secrets panel. The script downloads samples.json from the typesafe-public-examples repository if it isn’t already on disk. The file holds CLINC150 requests, used under CC BY 3.0. The attribution and a list of our changes are in DATA_LICENSE.md. A live run makes about 600 agent runs on Jev, and three on gpt-5.5, which reads a transfer’s amount. The latencies shown are the ones recorded when the answers were saved. The results below are from jev-1.14.0 and gpt-5.5, run on 2026-11-25.

Load a labeled sample

The requests come from CLINC150. Each one is a short request to an assistant, labeled with its intent: what the person wanted, such as balance. There are 150 intents across 10 domains, such as banking and travel, plus requests labeled out of scope because they fit none of the intents. The app handles the 30 intents in the banking and credit-card domains. Requests from the other eight domains, and the out-of-scope ones, should go to the decline route. samples.json holds 600 requests drawn at random, with a fixed seed, from the CLINC150 test set: 15 for each of the 30 intents, 75 from other domains, and 75 out of scope. The requests are short and casual, and a few intents overlap, so some requests fit two routes: transactions and spending_history, new_card and replacement_card_duration, credit_limit and balance.

Write each route as a function

Write each kind of request the app supports as a Python function. Each function is one route. Pydantic AI builds each route’s name, description, and argument types from the function’s signature and docstring. It then sends Jev one Choice question with every route as an option. A second call to Jev sends one Choice question for each argument of the picked route. To decide, Jev sees the request and any earlier turns, the agent’s instructions, and the route descriptions. Each route is an output function or a tool:
  • Output functions. Calling one ends the run, and its return value is the reply. They cover the 20 information requests and decline. Jev picks one and fills its arguments. The function then writes the answer from the customer’s data, so no model writes any text.
  • Tools with requires_approval=True, for the 10 actions. When Jev picks one, the run ends with a DeferredToolRequests object, which holds the tool call waiting for approval. The app shows a confirm button, and nothing runs until the customer taps it.
One tool, transfer, has an argument Jev can’t fill. Its amount: float | None can be any number, so there’s no set of options for Jev to choose from. When Jev picks transfer, Pydantic AI raises an UnfillableRoute exception. The app catches it and asks the customer for the amount. The account, card, and bill arguments are Choices built at run time from the customer’s data, so Jev picks from this customer’s own accounts instead of a list fixed in the code. The action functions return placeholder strings, and some output functions return fixed example text.
The other 26 routes follow the same patterns. The lists ANSWERS and ACTIONS hold every route, in the order Jev sees them. Each saved answer is stored under a key that includes WORDING, a short hash of the instructions, the customer data, and the route names, parameter names, and docstrings. Edit any of those and the saved answers no longer match, so the run needs new answers from the models.

Build the agent

The agent’s model is Jev. Its output types are the output functions plus DeferredToolRequests, which a run returns when it stops at an action. Its tools are the actions, each with requires_approval=True. Run the agent with decision_route_threshold=ROUTE_THRESHOLD. When the probability of Jev’s pick is under the threshold, Pydantic AI raises UnsureRoute before it fills any argument. The exception carries Jev’s probability for every route. The app offers the customer the routes at OFFER_FLOOR or more, and never fewer than two or more than four. respond() takes one customer request and returns a record of what the app does next: answer, show a confirm button, or ask which route. The record also holds Jev’s pick, the pick’s arguments, how long the run took, and every nonzero route probability, read from result.response.provider_details['route']['probabilities']. A run stops after the first call to Jev when the pick is under the threshold or is transfer.
The app turns each record into what the customer sees. When Jev is unsure, the app builds its question from ASK, a dict with a short phrase per route. Jev never sees the dict, but the question built from it goes into the history on the customer’s next turn.

Route a clear request

Jev picked bill_due and filled bill with internet. None of the customer’s bills mentions Comcast, but Comcast is an internet provider, so internet is the option that fits. The output function then wrote the answer from the bill data. The probability of the pick is 1.00, above the 0.8 threshold, so the app acted.

Ask the customer when Jev is unsure

Some requests could fit more than one route. A request about a utility bill:
Jev’s pick was bill_balance at 0.62, under the threshold, so Pydantic AI raised UnsureRoute and nothing ran. Three routes are at 0.1 or more: bill_balance, pay_bill at 0.25, and bill_due at 0.11. The app offered those three. The app sends the customer’s reply to the agent as the next turn, with the earlier turn in message_history, so Jev sees what the app asked. Jev then picked pay_bill at 1.00, and the run stopped at the confirm button. The customer didn’t say which bill, so bill is empty and the app can ask which bill before it shows the button.

Check that low-probability picks are wrong more often

Asking only helps if Jev’s low-probability picks are the ones it gets wrong. Run the 600 labeled requests, group Jev’s picks into bands by probability, and check how often the picks in each band match the label.
output output The share of picks that match the label rises with the probability, from 20% under 0.5 to 99% at 0.99 or more. About 80% of picks are at 0.99 or more. The bands under 0.9 hold few picks, so their shares are rough. We ask when the probability of the pick is under 0.8, and offer the routes at 0.1 or more. To choose your own values, try a few on labeled requests of your own, and compare how often the app asks with how often it acts on, or offers, the label’s route.

Count how the 600 requests end

A request ends correctly if the app acts on the label’s route, or asks and offers it. Compare that with acting on Jev’s pick every time:
The app ended 98.3% of the requests correctly, against 96.0% for acting on Jev’s pick every time. It acted without asking on 94% of the requests, and Jev’s pick matched the label on 98.4% of those. A request took a median of 0.16 seconds. The app asked on 35 requests. Jev’s pick matched the label on 20 of them, 57%, against 98.4% of the rest. The routes the app offered included the label’s route on 34 of the 35, at 2.3 routes per ask. Offering only Jev’s two likeliest routes would have included it on 31.

Confirm actions before they run

When Jev picks an action, the run stops before Pydantic AI calls the tool:
The run returned DeferredToolRequests instead of an answer. Jev picked report_lost_card, and requires_approval=True stopped the tool before it ran. The customer has two cards and didn’t say which, so card is None. Ask for the card before approving the call, because approval doesn’t check that the customer gave enough information. To run the tool once the customer confirms, keep the AgentRunResult and resume with agent.run_sync(message_history=result.all_messages(), deferred_tool_results=results). Here results is a DeferredToolResults whose approvals maps each pending tool call ID to True, or to ToolApproved(override_args=...) for corrected arguments.

Read a transfer’s amount with an LLM

Jev answers by choosing from options, so it can’t fill an argument that could be any number. When Jev picks transfer, Pydantic AI raises UnfillableRoute before it fills any argument:
The customer gave the accounts and the amount, and the app has none of them, so it has to ask. To have an LLM read them instead, put the LLM behind Jev in a FallbackModel. With fallback_on=UnfillableRoute, Pydantic AI calls gpt-5.5 only when Jev picks a route with an argument Jev can’t fill:
With gpt-5.5 behind Jev, the first request ends at a confirm button with the accounts and the amount filled in: gpt-5.5 read 360, from savings to checking. The second names no amount, so gpt-5.5 left amount empty and the app asks for it. Each of the three took two to three seconds. A request that never reaches gpt-5.5 takes about 0.2. FallbackModel hands gpt-5.5 the customer’s request and every route, so gpt-5.5 can overrule Jev’s pick. Pydantic AI called gpt-5.5 for the third request, so Jev had picked transfer. gpt-5.5 picked balance. To send the requests Jev is unsure about to gpt-5.5 too, instead of asking the customer, set fallback_on=DecisionHandOff, which covers both UnfillableRoute and UnsureRoute.

Open the request in the playground

To see what Pydantic AI sends Jev, give the agent a model that captures the call instead of sending it. Here we capture the route question for the Comcast request, with all 31 routes and their descriptions, and turn it into a playground link.
Open in the TypeSafe playground →