liveBook · Manning

Pre-model hook

As explained in AI Agents and Applications

A pre-model hook is a callback that runs before a language model invocation and can inspect or modify the model input.

A pre-model hook is a callback that runs before a language model invocation. It can inspect the current state and either leave the input unchanged or modify what the model receives. This makes it useful for enforcing scope and safety before an agent spends tokens or calls tools. section 14.2 section 14.2.2

A guardrail hook can enforce an agent boundary in four steps:

  • Read the latest message and confirm that it is a user message. section 14.2.2
  • Send the query to a classifier with a strict scope prompt, such as travel questions about Cornwall. section 14.2.2
  • Return an empty update for an in-scope request so normal processing continues. section 14.2.2
  • For an out-of-scope request, prepend a refusal instruction to the model input instead of allowing normal handling. section 14.2.2

Restricting a travel agent to Cornwall

Attach the same pre_model_guardrail to a travel-information agent and an accommodation-booking agent. A request for travel tips in Liverpool is classified as outside scope, so the assistant returns a polite refusal and suggests Cornwall destinations instead.

Listing 14.9 Agent-level guardrail function
def pre_model_guardrail(state: dict):
    messages = state.get("messages", [])
    last_msg = messages[-1] if messages else None
    if not isinstance(last_msg, HumanMessage):  #1
        return {}

    user_input = last_msg.content
    classifier_messages = [ #2
        SystemMessage(content=AGENT_GUARDRAIL_SYSTEM_PROMPT),
        HumanMessage(content=user_input),
    ]
    decision = llm_guardrail.invoke(classifier_messages)

    if decision.is_travel: #3
        # Allow normal flow; do not modify inputs
        return {}

    return {"llm_input_messages": 
        [SystemMessage(content=AGENT_REFUSAL_INSTRUCTION),
        *messages]}  #4

The implementation listing shows how the hook reads state, invokes a classifier, permits valid input, and injects refusal instructions for invalid input. section 14.2.2

How AI Agents and Applications covers it

How deep it goes
Explains state inspection, classification, pass-through, and refusal injection, while deferring post-model and tool-level checks.
Examples
Demonstrates Cornwall travel classification, refusal of Liverpool advice, and shared hooks on travel and accommodation agents.
What it assumes
Assumes familiarity with agent state, messages, classifiers, and model calls while demonstrating the hook logic directly.
What you can do after
Build input guardrails in LangGraph agents that classify requests and redirect out-of-scope inputs before model execution.
How it connects
Connects guardrails with language model invocation, agents, routing, tools, refusal handling, and post-model hooks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.