Examples

Check an expense against a policy

Open in playground →

A yes-or-no decision that needs a little reasoning first: thinking is on for that field only, and its reasoning comes back with the answer.

  • Boolean
  • Thinking

Challenges

  • The answer is not in the text. Boston is not New York, so the $180 cap applies, and $412 for two nights is $206 a night.
  • The policy also lists rules that do not apply here, for meals, alcohol and New York.
  • It takes a few steps before a yes or no, so thinking is on for that one field. The amount and the number of nights are read directly, without it.
import os
from typellm import TypeLLMClient

client = TypeLLMClient(api_key=os.environ["TYPELLM_API_KEY"])
response = client.generate(
    context="Travel policy: hotels are reimbursed up to $250 a night in New York and $180 a night elsewhere. Meals are reimbursed up to $75 a day. Alcohol is never reimbursed.\n\nExpense: two nights at the Marriott Boston, $412 in total, for a customer visit on 14 and 15 October.",
    questions={
      "amount": {
        "type": "number",
        "instructions": "Total amount claimed, in dollars."
      },
      "nights": {
        "type": "integer",
        "instructions": "How many hotel nights?"
      },
      "within_policy": {
        "type": "boolean",
        "instructions": "Is this expense within the travel policy?",
        "thinking": True,
        "thinking_budget": 1024
      }
    },
)
print(response)

Time 3.36 s · Cost $0.0001

Generation(
    result={
        'amount': 412,
        'nights': 2,
        'within_policy': False,
    },
    thinking={
        'within_policy': 'We need answer user\'s request. Need determine within_policy boolean for expense: two nights at Marriott Boston, $412 total, customer visit on 14 and 15 October. Policy: hotels reimbursed up to $250 a night in New York and $180 a night elsewhere. Meals up to $75/day. Alcohol never. Boston is elsewhere (not New York), cap $180/night. Two nights total $412 => average $206/night, exceeds $180 by $26/night, total allowed $360, expense $412. Thus not within policy. Output JSON {"within_policy": "B"}? Choices: {"A": true, "B": false}. Instructions answer as {"within_policy": "<label>"}. So label should be B for false. Need final only JSON. Ensure no extra.',
    },
    usage=Usage(input_tokens=154, thinking_tokens=189),
)