guidance

Constrain LLM output with regex and grammar rules for valid JSON, XML, and code.

Updated Apr 11, 2026
One-click install
npx skills add https://github.com/musical-basics/hermes-build-2 --skill guidance-musical-basics
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/musical-basics/hermes-build-2/tree/main/skills/mlops/inference/guidance
Command: npx skills add https://github.com/musical-basics/hermes-build-2 --skill guidance-musical-basics

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Guidance solves the problem of uncontrolled LLM outputs by constraining generation so that models produce syntactically valid and well-formed results (JSON, XML, code, dates, emails) without expensive retry loops or brittle post-validation.

Core Features & Use Cases

  • Regex and Grammar Constraints: Enforce token-level regex or CFG grammars so generated fields always match required formats.
  • Structured Output & Validation: Guarantee valid JSON/XML/code outputs and reduce downstream parsing errors.
  • Multi-Step Workflows & Agents: Build Pythonic, stateful or stateless workflows and ReAct-style agents that use selection constraints, token healing, and backend tools.
  • Use Case: Generate validated API payloads, extract structured entities from documents, or run constrained agents that call tools with validated inputs.

Quick Start

Use Guidance to generate a validated JSON person object containing name, age, and email.

Frequently Asked Questions about guidance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I guarantee valid JSON output from an LLM without using retry loops?

To guarantee valid JSON output from an LLM without retry loops, apply constrained generation using regex and grammar constraints. This enforces token-level syntax rules so generated fields always match required formats and eliminates brittle post-validation.

What is token healing and how does it help with structured LLM outputs?

Token healing is a constrained generation mechanism that repairs token boundaries during LLM generation. It ensures structured outputs like JSON, XML, and code remain syntactically valid by preventing partial token mismatches from breaking the required format.

Does constrained generation work with both local models and API backends?

Constrained generation works with both local models and API backends. You can apply grammar constraints, selection constraints, and backend configuration across Anthropic, OpenAI, Transformers, and llama.cpp to enforce valid structured outputs consistently.

How do I extract structured entities from documents using LLMs?

To extract structured entities from documents using LLMs, build Pythonic multi-step workflows with selection constraints. This forces the model to generate validated API payloads and structured data formats directly from the extracted text without parsing errors.

Can I build stateful ReAct-style agents that call tools with validated inputs?

You can build stateful or stateless ReAct-style agents that call tools with validated inputs. This is achieved by applying constrained generation, token healing, and selection constraints within multi-step workflows to ensure inputs match required formats.

Why does my LLM generate malformed JSON and how can I prevent it?

LLMs generate malformed JSON due to uncontrolled token generation. Prevent this by applying CFG grammars and token-level regex constraints during generation, which enforce well-formed structured outputs and eliminate downstream parsing errors.