Embedded DSLs

Semantic AI control flow

ai using context makes a workflow's Judge, Router, or Generator available to a block. The compiler checks the required capabilities before the first call. Calls in the block share the context's attempt and time limits.

Judge with feels

def review_content(content: Str, context: AiContext) -> Result[ReviewDecision, AiError] {
    ai using context {
        if content feels "contains a personal attack" with threshold 90% {
            return Ok(ReviewDecision.Flag)
        } uncertain {
            return Ok(ReviewDecision.HumanReview)
        } else {
            return Ok(ReviewDecision.Clear)
        }
    }
}

feels asks a Judge for a probability. The result can be Yes, No, or Uncertain, so the uncertain branch is required. Omit with threshold to use the context's threshold. Provider failures return AiError.

Route with by meaning

ai using context {
    match issue by meaning {
        case "a billing or payment problem" { team = Team.Billing }
        case "a software defect or technical failure" { team = Team.Technical }
        case _ { team = Team.General }
    } uncertain {
        team = Team.HumanReview
    }
}

The Router compares the descriptions. case _ handles a confident “none of these”; uncertain handles a tie, weak answer, or abstention. Ordinary match still matches literal patterns.

Generate text with llm

ai using context {
    let summary = llm "Summarize without adding facts." using source
}

llm sends the instruction and input to a Generator and returns text. Refusal, truncation, malformed output, and transport failures return typed errors. Generated text is data, not code to execute. A semantic while can judge and revise the new draft after each pass; see the AI library introduction for a full example.

The AI library guide shows how to create a context. Run the examples without a provider account using their offline fixtures:

env -u AI_GATEWAY_API_KEY vary run examples/ai-review
env -u AI_GATEWAY_API_KEY vary run examples/ai-triage
env -u AI_GATEWAY_API_KEY vary run examples/ai-refine

Fixtures check workflow wiring. Use a configured live run and Native AI evals to measure model behavior.

← Public API security
Native AI evals →