Skill metadata
Reference: full SKILL.md
The following is the complete skill definition that Mibyan loads when this skill is triggered. This is what the agent sees as instructions when the skill is active.
Guidance: Constrained LLM Generation
When to Use This Skill
Use Guidance when you need to:- Control LLM output syntax with regex or grammars
- Guarantee valid JSON/XML/code generation
- Reduce latency vs traditional prompting approaches
- Enforce structured formats (dates, emails, IDs, etc.)
- Build multi-step workflows with Pythonic control flow
- Prevent invalid outputs through grammatical constraints
Installation
Quick Start
Basic Example: Structured Generation
Chat format with a local model
Constraint support requires local logit access. Regex,select(), and grammar-based constrained generation only work with local backends (Transformers,LlamaCpp). Remote API backends (OpenAI, and Azure variants) support unconstrainedgen()/ chat only — they cannot enforce token-level constraints. guidance 0.3.x has nomodels.Anthropicclass.
Core Concepts
1. Context Managers
Guidance uses Pythonic context managers for chat-style interactions.- Natural chat flow
- Clear role separation
- Easy to read and maintain
2. Constrained Generation
Guidance ensures outputs match specified patterns using regex or grammars.Regex Constraints
- Regex converted to grammar at token level
- Invalid tokens filtered during generation
- Model can only produce matching outputs
Selection Constraints
3. Token Healing
Guidance automatically “heals” token boundaries between prompt and generation. Problem: Tokenization creates unnatural boundaries.- Natural text boundaries
- No awkward spacing issues
- Better model performance (sees natural token sequences)
4. Grammar-Based Generation
Define complex structures by composing grammar functions. The template-stringgrammar= form is not part of current guidance — build grammars from
composable functions, or use guidance.json() for JSON.
- Complex structured outputs
- Nested data structures
- Programming language syntax
- Domain-specific languages
5. Guidance Functions
Create reusable generation patterns with the@guidance decorator.
Backend Configuration
OpenAI (remote — unconstrained only)
Remote API backends cannot do constrained generation (regex/select/grammar);
use them only for plain chat/gen(). For constraints, use a local backend.
Local Models (Transformers)
Local Models (llama.cpp)
Common Patterns
Pattern 1: JSON Generation
Pattern 2: Classification
Pattern 3: Multi-Step Reasoning
Pattern 4: ReAct Agent
Pattern 5: Data Extraction
Best Practices
1. Use Regex for Format Validation
2. Use select() for Fixed Categories
3. Leverage Token Healing
4. Use stop Sequences
5. Create Reusable Functions
6. Balance Constraints
Comparison to Alternatives
When to choose Guidance:
- Need regex/grammar constraints
- Want token healing
- Building complex workflows with control flow
- Using local models (Transformers, llama.cpp)
- Prefer Pythonic syntax
- Instructor: Need Pydantic validation with automatic retrying
- Outlines: Need JSON schema validation
- LMQL: Prefer declarative query syntax
Performance Characteristics
Latency Reduction:- 30-50% faster than traditional prompting for constrained outputs
- Token healing reduces unnecessary regeneration
- Grammar constraints prevent invalid token generation
- Minimal overhead vs unconstrained generation
- Grammar compilation cached after first use
- Efficient token filtering at inference time
- Prevents wasted tokens on invalid outputs
- No need for retry loops
- Direct path to valid outputs
Resources
- Documentation: https://guidance.readthedocs.io
- GitHub: https://github.com/guidance-ai/guidance (18k+ stars)
- Notebooks: https://github.com/guidance-ai/guidance/tree/main/notebooks
- Discord: Community support available
See Also
references/constraints.md- Comprehensive regex and grammar patternsreferences/backends.md- Backend-specific configurationreferences/examples.md- Production-ready examples

