liveBook · Manning

Guidance

As explained in LLMs in Production

Guidance is an open-source library from Microsoft for enforcing programmatic constraints on language-model responses.

Guidance is an open-source Microsoft library for replacing “prompt and pray” with programmatic control over language-model output. It constrains response spaces, defines stopping tokens, and supports complex prompt templates. section 7.3.2

Its core building blocks combine a wrapped model with generation functions such as gen and select. Reusable grammars, defined with the @guidance decorator, express composable language rules that generated text must follow. section 7.3.2

Guidance is useful when unconstrained generation creates operational risk:

  • Set custom stopping tokens or regular-expression constraints for bounded outputs. section 7.3.2
  • Select from allowed values when a response must match a finite vocabulary. section 7.3.2
  • Attach tools that pause generation, execute a function, and resume generation with the result. section 7.4.1
  • Guard tool inputs with grammars, while still handling hallucinated tools, malformed arguments, and runtime errors. section 7.4.1

Constrained generation with Falcon

A wrapped Falcon model can generate a short continuation, stop at a newline or punctuation mark, select a color from ["cyan", "grey", "purple"], or produce text matching an email regular expression. The same approach supports a parts-of-speech grammar that selects labels such as Noun, Verb, Adjective, or Adverb for words in a sentence.

How LLMs in Production covers it

How deep it goes
Descends from basic generation controls to reusable grammars and tool execution, while deferring detailed documentation and broader reliability guarantees.
Examples
Demonstrates Falcon generation, stop conditions, finite selection, email matching, parts-of-speech tagging, and arithmetic tools.
What you can do after
Apply it via Guidance to build constrained generations, reusable grammars, and tool-using language-model workflows.
How it connects
Connects constrained generation with grammars, prompt engineering, language-model tools, LangChain, and fine-tuning.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.