Skip to content
Kavachकवच

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}")
syntax: Kavach code
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")
ask.kav: Kavach code, then what running it prints
  • ask needs the Ai capability, so it is in the agent's can.
  • Kavach builds a schema from Verdict and 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

WhatLimit or sizeSource
The modelclaude-haiku-5-5, unless the agent (model) or the call (model:) chooses one04-typed-ai-calls.md
An answer's length4,096 tokens, reasoning included; a Model's max_tokens changes it04-typed-ai-calls.md
An answer that doesn't fit the typeasked again 2 times, then InvalidOutput04-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 type T and asks again when it doesn't fit, so code never parses loose text.

    spec 8.4

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 questionInput tokensOutput tokensTime
Structured output (ask until now)227101,350 ms
A plain prompt asking for JSON4110885 ms
A forced tool call, as agent frameworks ask52634977 ms
Input tokensOutput tokensTime per callSame fields as structured output
Structured output421651,551–1,639 ms8 of 10 (one structured run against another)
The type in the prompt14980–971,144–1,198 ms7 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

QUIZ 1 / 1pick one
What can ask<Verdict>(…) give your code?