Course contentsShow
← AI Engineering
Lesson 527 of 1,88613. LLM Orchestration FrameworksFree lesson

Guidance: Constrained Generation Framework

Explore Guidance for controlling LLM outputs through templates, constraints, and programmatic generation.

Guidance: Constrained Generation Framework

What you'll learn: How to programmatically control LLM outputs using templates and constraints to generate structured, predictable results.

What Is Guidance?

Guidance is a framework that lets you write programs that interleave your code logic with LLM generation, giving you precise control over how and what the model generates. Think of it like mad libs for AI: you create a template with fixed parts and blanks, then constrain what the LLM can fill in those blanks.

Unlike traditional prompting where you send text and hope for the best, Guidance lets you enforce structure at generation time. You can require valid JSON, force specific formats, or even guide the model through multi-step reasoning where each step feeds into the next.

Why Constrained Generation Matters

When building production systems, you often need outputs in exact formats—JSON for APIs, specific answer patterns for downstream processing, or structured reasoning chains. Free-form generation is unpredictable. Guidance solves this by treating generation as a programmable process rather than a black box.

Analogy: Regular prompting is like asking someone to fill out a form and hoping they follow instructions. Guidance is like giving them a form with dropdown menus, checkboxes, and validation rules—they can't submit invalid data.

Core Concepts

  • Templates: Mix Python code and generation blocks
  • Constraints: Force outputs to match patterns (regex, grammars, options)
  • Programmatic flow: Use variables, loops, and conditionals during generation
  • Token healing: Automatically fix tokenization boundaries for better constraint adherence

Key Takeaway: Guidance transforms LLM generation from unpredictable text completion into programmable, constrained processes where you control structure, format, and flow at generation time—ideal for building reliable AI systems that need predictable outputs.