ask<T>
Also known as: llm prompt structured output json schema function calling typed response model openai claude retry
● Intermediate5 minBefore this: Agents, RecordsSpec 8.4
ask<T>(prompt) asks an AI model and gives back a checked value of type T, never loose text to parse.
const answer = try ask<Type>("the prompt, with {values}")type Verdict {
/// true if the review is positive
positive: Bool
/// one short reason
reason: String
}
agent Reader {
can Ai, Console
on review(text: String) ! Error {
const v = try ask<Verdict>("Is this review positive? {text}")
print("positive: {v.positive}")
}
}
fn main() {
const bot = Reader.start()
bot.review("Loved it, fast delivery") catch e => print("failed: {e}")
}The program ran and printed:
kavach run ask.kav
positive: true[dry run] ask("Is this review positive? Loved it, fast delivery") -> Verdict(positive: true, reason: " fry ey")
askneeds theAicapability, so it is in the agent'scan.- Kavach builds a schema from
Verdictand sends it with the prompt. The///comments describe each field to the model. - The answer is checked against
Verdict. A wrong answer is asked again, twice by default, then fails.
This output comes from kavach run --dry-run: no model is called and nothing costs. ask gets a made-up Verdict, the same on every run, and the last line shows the simulated call. Without --dry-run, the model the code chooses answers: see Choosing the model.
Limits and sizes
| What | Limit or size | Source |
|---|---|---|
The model | claude-haiku-5-5, unless the agent (model) or the call (model:) chooses one | 04-typed-ai-calls.md |
An answer's length | 4,096 tokens, reasoning included; a Model's max_tokens changes it | 04-typed-ai-calls.md |
An answer that doesn't fit the type | asked again 2 times, then InvalidOutput | 04-typed-ai-calls.md |
Every limit in Kavach, on one page: Limits and Sizes.
One page for each form
What Kavach does for you
AI answers are typed, not text
ask<T>checks the model's answer against the typeTand asks again when it doesn't fit, so code never parses loose text.
Go deeper the why, for readers who want it
How ask asks: the type in the prompt
The first try writes T into the prompt in a short form: its fields, their types, the choices of a union, and the /// notes. Kavach checks the answer against T. Only an answer that doesn't fit is asked again in the provider's structured-output mode, which holds the model to the type. Measured when this was decided:
| One yes-or-no question | Input tokens | Output tokens | Time |
|---|---|---|---|
| Structured output (ask until now) | 227 | 10 | 1,350 ms |
| A plain prompt asking for JSON | 41 | 10 | 885 ms |
| A forced tool call, as agent frameworks ask | 526 | 34 | 977 ms |
| Input tokens | Output tokens | Time per call | Same fields as structured output | |
|---|---|---|---|---|
| Structured output | 421 | 65 | 1,551–1,639 ms | 8 of 10 (one structured run against another) |
| The type in the prompt | 149 | 80–97 | 1,144–1,198 ms | 7 and 9 of 10, in two runs |
So answers are as good, for about a third of the input tokens. How this adds up on whole tasks, against Python's usual ways: Agents in Practice.
Check yourself
ask<Verdict>(…) give your code?