Guidance — Constrained LLM Generation

You are an expert in Guidance, Microsoft's library for controlling LLM output with constrained generation. You help developers write programs that interleave text generation with control flow (loops, conditionals, regex constraints, JSON schemas, function calls) — ensuring LLM output always matches the expected format by constraining the token generation process itself, not just prompting.

25 stars

Best use case

Guidance — Constrained LLM Generation is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

You are an expert in Guidance, Microsoft's library for controlling LLM output with constrained generation. You help developers write programs that interleave text generation with control flow (loops, conditionals, regex constraints, JSON schemas, function calls) — ensuring LLM output always matches the expected format by constraining the token generation process itself, not just prompting.

Teams using Guidance — Constrained LLM Generation should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/guidance/SKILL.md --create-dirs "https://raw.githubusercontent.com/ComeOnOliver/skillshub/main/skills/TerminalSkills/skills/guidance/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/guidance/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How Guidance — Constrained LLM Generation Compares

Feature / AgentGuidance — Constrained LLM GenerationStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

You are an expert in Guidance, Microsoft's library for controlling LLM output with constrained generation. You help developers write programs that interleave text generation with control flow (loops, conditionals, regex constraints, JSON schemas, function calls) — ensuring LLM output always matches the expected format by constraining the token generation process itself, not just prompting.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Guidance — Constrained LLM Generation

You are an expert in Guidance, Microsoft's library for controlling LLM output with constrained generation. You help developers write programs that interleave text generation with control flow (loops, conditionals, regex constraints, JSON schemas, function calls) — ensuring LLM output always matches the expected format by constraining the token generation process itself, not just prompting.

## Core Capabilities

### Constrained Generation

```python
import guidance
from guidance import models, gen, select, regex, one_or_more, zero_or_more

# Load model (local or API)
lm = models.OpenAI("gpt-4o")
# Or local: models.Transformers("meta-llama/Llama-3.1-8B-Instruct")

# Simple constrained generation
lm += f"""
Classify this review sentiment.
Review: "The product arrived damaged but customer service was great"

Sentiment: {select(["positive", "negative", "mixed", "neutral"], name="sentiment")}
Confidence: {gen(regex=r"0\.\d{2}", name="confidence")}
"""
print(lm["sentiment"])     # "mixed" — constrained to exactly these options
print(lm["confidence"])    # "0.82" — matches regex pattern exactly

# Structured extraction with loops
lm += f"""Extract all people mentioned:
Text: "Alice met Bob at the cafe. Charlie joined them later."

People:
{one_or_more(f'''
- Name: {gen(regex=r"[A-Z][a-z]+", name="names", list_append=True)}
''')}
"""
print(lm["names"])         # ["Alice", "Bob", "Charlie"]
```

### JSON Generation

```python
# Guaranteed valid JSON output
from guidance import json as gen_json
from pydantic import BaseModel

class ProductReview(BaseModel):
    product_name: str
    rating: int                           # Constrained to int
    pros: list[str]
    cons: list[str]
    recommendation: bool

lm += f"""Analyze this review and extract structured data:
Review: "The XPS 15 has an amazing display and battery life, but runs hot under load. Would buy again."

{gen_json(schema=ProductReview, name="review")}
"""

review = lm["review"]
# {"product_name": "XPS 15", "rating": 4, "pros": ["amazing display", "battery life"],
#  "cons": ["runs hot under load"], "recommendation": true}
# GUARANTEED valid JSON matching the Pydantic schema
```

### Control Flow

```python
# Branching based on LLM output
lm += f"""
Task: {user_input}

First, determine the task type: {select(["question", "command", "chitchat"], name="task_type")}
"""

if lm["task_type"] == "question":
    lm += f"""
Answer the question with evidence:
Answer: {gen(max_tokens=200, name="answer")}
Sources: {gen(regex=r"https?://\S+", name="source")}
"""
elif lm["task_type"] == "command":
    lm += f"""
Generate the command:
```bash
{gen(stop="```", name="command")}
```
Explanation: {gen(max_tokens=100, name="explanation")}
"""
else:
    lm += f"Response: {gen(max_tokens=50, name="response")}"

# Multi-step reasoning
lm += f"""
Problem: {math_problem}

Let me solve this step by step:
{one_or_more(f'''
Step {gen(regex=r"\d+", name="step_num")}: {gen(stop="\n", name="steps", list_append=True)}
''')}

Final answer: {gen(regex=r"-?\d+\.?\d*", name="answer")}
"""
```

## Installation

```bash
pip install guidance
```

## Best Practices

1. **Select for classification** — Use `select()` instead of free-form text; LLM can only output valid options
2. **Regex for format** — Use `regex=` for dates, numbers, IDs; output always matches the pattern
3. **JSON schema** — Use `gen_json(schema=...)` for structured data; impossible to generate invalid JSON
4. **Local models** — Guidance works best with local models (full token control); API models use prompt-based constraints
5. **Control flow** — Mix Python logic with generation; branch on LLM output, loop for extraction
6. **Named captures** — Use `name=` parameter to capture generated values; access with `lm["name"]`
7. **Stop tokens** — Use `stop=` to control generation boundaries; prevent runaway output
8. **List extraction** — Use `one_or_more()` with `list_append=True` for extracting variable-length lists

Related Skills

apify-lead-generation

25
from ComeOnOliver/skillshub

Generates B2B/B2C leads by scraping Google Maps, websites, Instagram, TikTok, Facebook, LinkedIn, YouTube, and Google Search. Use when user asks to find leads, prospects, businesses, build lead lists, enrich contacts, or scrape profiles for sales outreach.

podcast-generation

25
from ComeOnOliver/skillshub

Generate AI-powered podcast-style audio narratives using Azure OpenAI's GPT Realtime Mini model via WebSocket. Use when building text-to-speech features, audio narrative generation, podcast creation from content, or integrating with Azure OpenAI Realtime API for real audio output. Covers full-stack implementation from React frontend to Python FastAPI backend with WebSocket streaming.

documentation-generation-doc-generate

25
from ComeOnOliver/skillshub

You are a documentation expert specializing in creating comprehensive, maintainable documentation from code. Generate API docs, architecture diagrams, user guides, and technical references using AI-powered analysis and industry best practices.

ai-video-generation

25
from ComeOnOliver/skillshub

Generate AI videos with Google Veo, Seedance, Wan, Grok and 40+ models via inference.sh CLI. Models: Veo 3.1, Veo 3, Seedance 1.5 Pro, Wan 2.5, Grok Imagine Video, OmniHuman, Fabric, HunyuanVideo. Capabilities: text-to-video, image-to-video, lipsync, avatar animation, video upscaling, foley sound. Use for: social media videos, marketing content, explainer videos, product demos, AI avatars. Triggers: video generation, ai video, text to video, image to video, veo, animate image, video from image, ai animation, video generator, generate video, t2v, i2v, ai video maker, create video with ai, runway alternative, pika alternative, sora alternative, kling alternative

ai-image-generation

25
from ComeOnOliver/skillshub

Generate AI images with FLUX, Gemini, Grok, Seedream, Reve and 50+ models via inference.sh CLI. Models: FLUX Dev LoRA, FLUX.2 Klein LoRA, Gemini 3 Pro Image, Grok Imagine, Seedream 4.5, Reve, ImagineArt. Capabilities: text-to-image, image-to-image, inpainting, LoRA, image editing, upscaling, text rendering. Use for: AI art, product mockups, concept art, social media graphics, marketing visuals, illustrations. Triggers: flux, image generation, ai image, text to image, stable diffusion, generate image, ai art, midjourney alternative, dall-e alternative, text2img, t2i, image generator, ai picture, create image with ai, generative ai, ai illustration, grok image, gemini image

when-creating-presentations-use-pptx-generation

25
from ComeOnOliver/skillshub

Enterprise-grade PowerPoint deck generation using evidence-based prompting, workflow enforcement, constraint-based design

pptx-generation

25
from ComeOnOliver/skillshub

Enterprise-grade PowerPoint deck generation system using evidence-based prompting techniques, workflow enforcement, and constraint-based design. Use when creating professional presentations (board decks, reports, analyses) requiring consistent visual quality, accessibility compliance, and integration of complex data from multiple sources. Implements html2pptx workflow with spatial layout optimization, validation gates, and multi-chat architecture for 30+ slide decks.

agent-generation

25
from ComeOnOliver/skillshub

This skill provides knowledge for generating effective Claude Code agents tailored to specific projects. It is used internally by the agent-team-creator plugin when analyzing codebases and creating specialized agent teams. Contains templates, best practices, and patterns for writing project-aware agents.

video-generation-skill

25
from ComeOnOliver/skillshub

Design video concepts, scripts, shotlists, transitions, and editing notes for VEO, Gemini, and Nano Banana-based pipelines. Use when turning a marketing idea into concrete video assets.

music-generation

25
from ComeOnOliver/skillshub

Tools, patterns, and utilities for generating professional music with realistic instrument sounds. Write custom compositions using music21 or learn from existing MIDI files.

document-generation

25
from ComeOnOliver/skillshub

A powerful skill for generating and processing professional documents (Word, PowerPoint, Excel, PDF).

Vertex Media Generation

25
from ComeOnOliver/skillshub

## Overview