Agentic AI

An agent in one script

The whole module in one Python file: an email assistant on Gemini that reads the inbox with tools, asks you before it sends anything, remembers the conversation and summarises it when it grows. Read the script to revise; press Explain on it for every step explained.

Concept + code Python 3.12 Needs GOOGLE_API_KEY

Lessons 9.1 to 9.7 covered the parts of a LangChain 1 agent: a model, messages, tools, the agent loop with its memory, structured output and middleware. This lesson puts them together in one script, with Gemini as the model: an email assistant that can read the inbox freely but must ask you before sending. It is the page to come back to for the syntax.

Before you run it

  • GOOGLE_API_KEY in your .env file, created in Google AI Studio (lesson 6.1).
  • The packages of lesson 6.1's requirements.txt; the script uses langchain, langchain-google-genai and langgraph, which langchain installs.

The script

email_agent.pyPython
"""An email assistant: reads the inbox, asks before sending, summarises long conversations."""
import uuid

from dotenv import load_dotenv
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware, SummarizationMiddleware
from langchain.chat_models import init_chat_model
from langchain.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.types import Command

load_dotenv()  # GOOGLE_API_KEY from .env

# 1. Model: one function for every provider (lesson 9.1)
model = init_chat_model("google_genai:gemini-3.5-flash-lite")

# 2. Tools: functions the model may ask for (lesson 9.3)
INBOX = {
    "1": {"sender": "alice@example.com", "subject": "Meeting moved", "body": "Can we move tomorrow's meeting to 3 pm?"},
    "2": {"sender": "bob@example.com", "subject": "Invoice 1042", "body": "Please confirm you received invoice 1042."},
}


@tool
def read_email_tool(email_id: str) -> str:
    """Read an email from the inbox by its ID."""
    email = INBOX.get(email_id)
    if email is None:
        return f"No email with ID {email_id}. Valid IDs: {', '.join(INBOX)}"
    return f"From: {email['sender']}\nSubject: {email['subject']}\n\n{email['body']}"


@tool
def send_email_tool(recipient: str, subject: str, body: str) -> str:
    """Send an email."""
    return f"Email sent to {recipient} with subject '{subject}'"


# 3. Agent: the loop, its memory and its middleware (lessons 9.4, 9.6, 9.7)
agent = create_agent(
    model=model,
    tools=[read_email_tool, send_email_tool],
    system_prompt=(
        "You are an email assistant. The inbox has emails with IDs "
        f"{', '.join(INBOX)}. Read an email before you reply to it. Keep replies short."
    ),
    middleware=[
        SummarizationMiddleware(model=model, trigger=("tokens", 4000), keep=("messages", 10)),
        HumanInTheLoopMiddleware(
            interrupt_on={"send_email_tool": {"allowed_decisions": ["approve", "edit", "reject"]}}
        ),
    ],
    checkpointer=InMemorySaver(),
)


# 4. Review: one decision per paused tool call (lesson 9.7)
def ask_decision(action: dict) -> dict:
    print(f"\nApprove {action['name']}? {action['args']}")
    choice = input("Approve (y), edit the body (e) or reject (n)? ").strip().lower()
    if choice == "y":
        return {"type": "approve"}
    if choice == "e":
        args = {**action["args"], "body": input("New body: ")}
        return {"type": "edit", "edited_action": {"name": action["name"], "args": args}}
    return {"type": "reject", "message": input("Why not? ")}


def main() -> None:
    config = {"configurable": {"thread_id": str(uuid.uuid4())}}
    print("Email assistant. Type /quit to stop.")
    while True:
        text = input("\nYou: ").strip()
        if text == "/quit":
            break
        if not text:
            continue
        result = agent.invoke({"messages": [{"role": "user", "content": text}]}, config)
        while "__interrupt__" in result:
            requests = result["__interrupt__"][0].value["action_requests"]
            decisions = [ask_decision(action) for action in requests]
            result = agent.invoke(Command(resume={"decisions": decisions}), config)
        print(f"Assistant: {result['messages'][-1].text}")


if __name__ == "__main__":
    main()

Run it

Terminalzsh
python email_agent.py
OutputIllustrative · the answers vary per run
Email assistant. Type /quit to stop.

You: What's in email 1?
Assistant: Alice asks whether tomorrow's meeting can move to 3 pm.

You: Tell her that works for me.

Approve send_email_tool? {'recipient': 'alice@example.com', 'subject': 'Re: Meeting moved', 'body': '3 pm works for me.'}
Approve (y), edit the body (e) or reject (n)? e
New body: 3 pm works for me. See you then.
Assistant: Done: I replied to Alice that 3 pm works.

You: Now confirm Bob's invoice.

Approve send_email_tool? {'recipient': 'bob@example.com', 'subject': 'Re: Invoice 1042', 'body': 'I confirm receipt of invoice 1042.'}
Approve (y), edit the body (e) or reject (n)? n
Why not? Finance confirms invoices, not me.
Assistant: Understood, I didn't send it. Finance will need to confirm invoice 1042.

You: /quit

The first question needed only the reading tool, so it ran without a pause. "Tell her" worked because the checkpointer kept the first exchange. For Bob's invoice the model read email 2 on its own before proposing a reply, as the system prompt asks.

Swap one piece

Part In the script Alternatives
Model "google_genai:gemini-3.5-flash-lite" "openai:gpt-5", "groq:openai/gpt-oss-120b" (9.1)
Memory InMemorySaver() A database checkpointer such as SqliteSaver, so threads survive restarts
Shortening the history SummarizationMiddleware Trimming in a before_model hook, or a ("fraction", 0.7) trigger (9.6)
Approval Batched decisions for one tool True for all decisions, a when condition, interrupt_mode="per_call" (9.7)
Answer format Text A Pydantic response_format (9.5)

The same memory with the classic API is the chatbot of lesson 7.5.

Practice

Practice 1Start a new conversation. Add a /new command that starts a fresh thread, so the assistant forgets the conversation without restarting the script.Show solution
email_agent.py (in the loop, before the empty-text check)Python
        if text == "/new":
            config = {"configurable": {"thread_id": str(uuid.uuid4())}}
            print("New conversation.")
            continue

A new thread ID means an empty history: the checkpointer still holds the old thread, but nothing points to it any more.

Practice 2Gate another tool. Add a delete_email_tool(email_id) that removes an email from INBOX, and make it pause for approve or reject only: editing the ID of a deletion would be a second way to delete.Show solution
email_agent.pyPython
@tool
def delete_email_tool(email_id: str) -> str:
    """Delete an email from the inbox by its ID."""
    if INBOX.pop(email_id, None) is None:
        return f"No email with ID {email_id}."
    return f"Deleted email {email_id}."


# in create_agent
    tools=[read_email_tool, send_email_tool, delete_email_tool],
# in HumanInTheLoopMiddleware
            interrupt_on={
                "send_email_tool": {"allowed_decisions": ["approve", "edit", "reject"]},
                "delete_email_tool": {"allowed_decisions": ["approve", "reject"]},
            }

ask_decision needs no change for approve and reject. If the reviewer types e for a deletion, resuming raises ValueError: ... Decision type 'edit' is not allowed for tool 'delete_email_tool', which ends the script. The allowed decisions travel with each pause, in result["__interrupt__"][0].value["review_configs"]; offer only those.