Agentic AI

An approval agent in one script

The module in one Python file: a calculator agent on Gemini that stops before every tool call for your approve, edit or reject, and lets you list its checkpoints and undo your last question. Read it to revise; press Explain on it for every step.

Concept + code Python 3.12 Needs GOOGLE_API_KEY

Lessons 14.1 to 14.4 paused a graph, edited its state, asked a person with interrupt and travelled back through its checkpoints. This script puts the pieces together around the arithmetic agent of lesson 14.1: the model may only use a tool after you have seen the call and approved, edited or rejected it, /history lists the conversation's checkpoints, and /undo takes back your last question. It is the graph-level counterpart of lesson 9.8's email assistant, which gets its pause from middleware.

Before you run it

The script

approval_agent.pyPython
"""A calculator agent that asks before every tool call, with history and undo. Gemini decides."""
import json
import uuid
from typing import Literal

from dotenv import load_dotenv
from langchain_core.messages import SystemMessage, ToolMessage
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, MessagesState, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition
from langgraph.types import Command, interrupt

load_dotenv()  # GOOGLE_API_KEY from .env


# 1. Tools: three plain functions; names, hints and docstrings become their schemas
def add(a: int, b: int) -> int:
    """Adds a and b."""
    return a + b


def multiply(a: int, b: int) -> int:
    """Multiply a and b."""
    return a * b


def divide(a: int, b: int) -> float:
    """Divide a by b."""
    return a / b


tools = [add, multiply, divide]

# 2. Model, with the tools' schemas attached to every request
llm = ChatGoogleGenerativeAI(model="gemini-3.5-flash-lite")
llm_with_tools = llm.bind_tools(tools)
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.")


# 3. Nodes: the model, and a review that pauses before any tool runs
def assistant(state: MessagesState) -> dict:
    return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}


def review(state: MessagesState) -> Command[Literal["tools", "assistant"]]:
    request = state["messages"][-1]
    decision = interrupt({"tool_calls": request.tool_calls})
    if decision["type"] == "approve":
        return Command(goto="tools")
    if decision["type"] == "edit":
        edited = request.model_copy(update={"tool_calls": decision["tool_calls"]})
        return Command(goto="tools", update={"messages": [edited]})
    refusals = [ToolMessage(content=f"Not run: {decision['reason']}", tool_call_id=call["id"], status="error")
                for call in request.tool_calls]
    return Command(goto="assistant", update={"messages": refusals})


# 4. Graph: assistant -> review -> tools -> assistant ... until the model answers
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant)
builder.add_node("review", review)
builder.add_node("tools", ToolNode(tools, handle_tool_errors=True))
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition, {"tools": "review", END: END})
builder.add_edge("tools", "assistant")
graph = builder.compile(checkpointer=MemorySaver())


# 5. The person's side: one decision for the tool calls of one model reply
def read_args(call: dict) -> dict:
    while True:
        text = input(f"  New arguments for {call['name']}, as JSON: ")
        try:
            return json.loads(text)
        except json.JSONDecodeError:
            print('  Not JSON. Example: {"a": 2, "b": 3}')


def ask(tool_calls: list[dict]) -> dict:
    for call in tool_calls:
        print(f"  ? {call['name']}({call['args']})")
    choice = input("Run it? (y)es, (e)dit, (n)o: ").strip().lower()
    if choice == "y":
        return {"type": "approve"}
    if choice == "e":
        return {"type": "edit", "tool_calls": [{**call, "args": read_args(call)} for call in tool_calls]}
    return {"type": "reject", "reason": input("Why not? ")}


# 6. Time travel: the current branch of checkpoints, newest first, and undo
def branch(config: dict):
    snapshot = graph.get_state(config)
    while snapshot.metadata:
        yield snapshot
        if snapshot.parent_config is None:
            break
        snapshot = graph.get_state(snapshot.parent_config)


def show_history(config: dict) -> None:
    for snapshot in branch(config):
        meta = snapshot.metadata
        count = len(snapshot.values.get("messages", []))
        print(f"  step {meta['step']:>2}  {meta['source']:<6} next={str(snapshot.next):<15} messages={count}")


def undo(config: dict) -> bool:
    last_input = next((s for s in branch(config) if s.metadata["source"] == "input"), None)
    if last_input is None or last_input.parent_config is None:
        return False
    graph.update_state(last_input.parent_config, None)
    return True


def main() -> None:
    # 7. Chat loop: run each question, answer every pause, print the answer
    config = {"configurable": {"thread_id": str(uuid.uuid4())}}
    print("Ask for some arithmetic. Commands: /history, /undo, /quit.")
    while True:
        text = input("You: ").strip()
        if text == "/quit":
            break
        if text == "/history":
            show_history(config)
            continue
        if text == "/undo":
            print("Back to before your last question." if undo(config) else "Nothing to undo.")
            continue
        if not text:
            continue
        result = graph.invoke({"messages": text}, config)
        while "__interrupt__" in result:
            decision = ask(result["__interrupt__"][0].value["tool_calls"])
            result = graph.invoke(Command(resume=decision), config)
        print(f"Bot: {result['messages'][-1].text}")


if __name__ == "__main__":
    main()

Run it

Terminalzsh
python approval_agent.py
OutputIllustrative · answers and tool calls vary per run; history lines trimmed
Ask for some arithmetic. Commands: /history, /undo, /quit.
You: Multiply 2 and 3
  ? multiply({'a': 2, 'b': 3})
Run it? (y)es, (e)dit, (n)o: y
Bot: 2 multiplied by 3 is 6.
You: Now divide that by 4
  ? divide({'a': 6, 'b': 4})
Run it? (y)es, (e)dit, (n)o: e
  New arguments for divide, as JSON: {"a": 6, "b": 3}
Bot: 6 divided by 3 is 2.
You: Add 10 to it
  ? add({'a': 2, 'b': 10})
Run it? (y)es, (e)dit, (n)o: n
Why not? Add 100 instead
  ? add({'a': 2, 'b': 100})
Run it? (y)es, (e)dit, (n)o: y
Bot: 2 plus 100 is 102.
You: /undo
Back to before your last question.
You: /history
  step 11  update next=()              messages=8
  step 10  loop   next=()              messages=8
  step  9  loop   next=('assistant',)  messages=7
  [...]
  step  0  loop   next=('assistant',)  messages=1
  step -1  input  next=('__start__',)  messages=0
You: Divide 5 by 0
  ? divide({'a': 5, 'b': 0})
Run it? (y)es, (e)dit, (n)o: y
Bot: I can't divide 5 by 0: division by zero is undefined.
You: /quit
  • The edit was silent. The model found divide(6, 3) in the history as if it had asked for it, and answered for 3 although the question said 4. To let the model know, add a note to the tool's result, as LangChain 1.4's middleware does (lesson 9.7).
  • The rejection had a reason. "Not run: Add 100 instead" reached the model as the tool's result, and it asked again with the new numbers, which paused again.
  • /undo took back the whole turn, the rejection included. The branch now ends with an update checkpoint holding the eight messages of the first two turns, and the next question continued from there.
  • The division by zero raised inside the tool; handle_tool_errors=True turned it into an error message, and the model explained it.

Swap one piece

Piece In the script Alternatives
Model ChatGoogleGenerativeAI(...) The Groq, OpenAI and Ollama tabs of lesson 14.1
The pause interrupt in a review node interrupt_before=["tools"] and update_state (lessons 14.1, 14.2); for create_agent agents, HumanInTheLoopMiddleware (lesson 9.7)
What pauses Every tool call Only some: return Command(goto="tools") before interrupt when a call is harmless (practice 1)
Memory MemorySaver, one thread per run SqliteSaver or PostgresSaver, so paused runs survive restarts (lesson 11.5)
Rewind Undo the last question Replay the last model call for another answer (practice 2), or fork with changed state (lesson 14.4)
The person's side input() in a terminal A web page or a review queue that resumes the thread by its ID; the graph doesn't change

Practice

Practice 1Approve small sums automatically. Make review let calls through without asking when every argument of every call is below 100.Show solution
approval_agent.py (at the top of review)Python
    request = state["messages"][-1]
    if all(abs(value) < 100 for call in request.tool_calls for value in call["args"].values()):
        return Command(goto="tools")
    decision = interrupt({"tool_calls": request.tool_calls})

The check runs before interrupt and gives the same answer every time the node runs on the same state, so it is safe on resume. Its condition depends only on the state, never on something that can change between the pause and the resume, such as the time.

Practice 2Retry the last answer. Add a /retry command that asks the model again for its last answer: find the newest checkpoint on the current branch whose next is ("assistant",), replay from it, and handle its pauses like a question.Show solution
approval_agent.py (in the loop, after /undo)Python
        if text == "/retry":
            before = next((s for s in branch(config) if s.next == ("assistant",)), None)
            if before is None:
                print("Nothing to retry.")
                continue
            result = graph.invoke(None, before.config)
            while "__interrupt__" in result:
                decision = ask(result["__interrupt__"][0].value["tool_calls"])
                result = graph.invoke(Command(resume=decision), config)
            print(f"Bot: {result['messages'][-1].text}")
            continue

The replay starts a branch at that checkpoint (lesson 14.4): the model runs again on the same messages, and if it asks for a tool, review pauses again. Resuming with the plain thread config works because the replay's pause is now the thread's latest checkpoint. The earlier answer stays in the history, on the old branch.