
The simplest use of Vary's AI library is an ordinary LLM call: give a model
instructions and input text, and get text back. You can also add a Judge or
Router when a workflow needs decisions. The review example shows what happens
when a Judge is unsure; the drafting example combines generation and review.

| Topic | What this article shows |
| --- | --- |
| AI library basics | Contexts, capabilities, adapters, and shared limits. |
| Normal LLM calls | Summarize text or draft a reply with a Generator. |
| Content review | Use a Judge and handle an uncertain answer. |
| Draft refinement | Combine generation and judgment in one bounded workflow. |
| Evals | Check expected decisions against scripted cases. |

The compiler understands `ai using`, `feels`, `llm`, and `eval`. The bundled
`ai` library supplies the context, adapters, budgets, and result types behind
that syntax.

The review and refinement applications have scripted responses. You can
inspect their behaviour without a provider account.

## What the library provides

An `AiContext` holds a workflow's capabilities and limits. The review app uses
a Judge. The refinement app uses a Judge and a Generator. Scripted versions of
those capabilities make both examples runnable offline.

A **Generator** is the capability for a normal LLM text response. `llm ...
using ...` calls that capability through the current context. A **Judge**
answers a yes/no question, and a **Router** selects among descriptions. An app
can use any one of these capabilities or combine them; generation does not
require a decision model.

For live calls, the TypeSafe adapter handles judgment and routing; the Chat
Completions adapter handles generation. A binding names its endpoint and model.
The credential stays with the client. The context counts attempts and records
a trace. Typed eval cases and fixtures come from the library too.

When an app needs a Judge and a Router together, `typesafe_decisions` creates
both bindings. `decision_context` gives them one budget and policy. Any adapter
that supplies the same typed pair can use that context constructor. The
refinement example needs a different combination, Judge and Generator, so it
assembles those bindings directly.

Jev is one model you can configure for the TypeSafe adapter. The
[Jev article](/articles/jev-ai-with-vary/) shows that setup.

## A normal LLM call

Suppose an app needs a one-sentence summary of a customer message. With a
Generator in its context, the workflow can make that call directly:

```vary-snippet
def summarize(message: Str, context: AiContext) -> Result[Str, AiError] {
    ai using context {
        let summary = llm "Summarize in one sentence. Keep the facts." using message
        return Ok(summary)
    }
}
```

The text after `llm` is the instruction; `message` is the input sent with it.
The result is generated text, or an `AiError` if the call fails. This function
does not ask the model to judge or route anything. The same operation is
available as an ordinary library call when the DSL is not needed:

```vary-snippet
from ai.capabilities import GenerationInput

def summarize(message: Str, context: AiContext) -> Result[Str, AiError] {
    return context.generate(
        GenerationInput("Summarize in one sentence. Keep the facts.", message)
    )
}
```

The same Generator can draft a reply instead of a summary. Only the
instruction and input change:

```vary-snippet
def draft_reply(message: Str, context: AiContext) -> Result[Str, AiError] {
    ai using context {
        let reply = llm "Draft a brief, helpful reply. Do not promise a fix." using message
        return Ok(reply)
    }
}
```

For a live LLM, the Chat Completions adapter supplies the Generator binding;
the configured endpoint and model select the service to call. The adapter is
separate from the decision-oriented TypeSafe adapter. The next example uses
only a Judge, and the draft refinement example combines a Judge with ordinary
LLM generation.

## A review decision with room for uncertainty

`examples/ai-review/src/workflow.vary` returns a decision and leaves the
content alone:

```vary-snippet
ai using context {
    if content feels "contains a personal attack directed at a person" with threshold 90% {
        return Ok(ReviewReport(ReviewDecision.Flag, content))
    } uncertain {
        return Ok(ReviewReport(ReviewDecision.HumanReview, content))
    } else {
        return Ok(ReviewReport(ReviewDecision.Clear, content))
    }
}
```

`review_content` gets its Judge from the context. A confident yes flags the
content; a no clears it. Uncertainty sends it to a person. A provider failure
returns `AiError`.

The checked-in fixture flags an attack. That result comes from a scripted
response, not a live model.

## A draft that can ask for another pass

`examples/ai-refine/src/workflow.vary` puts generation and judgment under one
context. Here is the drafting loop:

```vary-snippet
ai using context {
    mut draft = llm "Write a concise summary. Preserve facts and qualifications." using source
    while draft feels "repetitive or unnecessarily wordy" {
        draft = llm "Remove repetition. Preserve meaning, facts, and qualifications." using draft
    } uncertain {
        review = ReviewState.NeedsCloserReview
    }
    let title = llm "Write a short descriptive title. Add no new facts." using draft
    return Ok(EditReport(draft, title, review))
}
```

Each `llm ... using` call sends an instruction and its input text. The `while`
condition judges the latest draft. If the Judge is uncertain, the report asks
for closer review. One budget covers all calls, with at most nine attempts and
three revisions.

The fixture returns a clear draft and a short title. The Judge and Generator
are both scripted here.

## Check a workflow with an eval

The review example has two typed eval cases: an attack expected to be flagged
and a neutral message expected to be cleared. `src/evals.vary` scores the
workflow's decision:

```vary-snippet
eval "content review decision" {
    cases review_cases
    run review_content
    score decision_matches
    require { pass_rate >= 50% error_rate == 0% }
}
```

The fixture eval runs two cases, and both pass. It checks the workflow and its
scorer against scripted answers. Live model behaviour is a separate question.

For syntax, see [Semantic AI control flow](/docs/ai-semantic-control-flow/)
and [Native AI evals](/docs/ai-evals/). The [AI library guide](/docs/ai-library/)
covers contexts, adapters, and budgets. The next article configures
[Jev AI through the TypeSafe adapter](/articles/jev-ai-with-vary/).
