Zero Labs
Industry News / AI

Learning Deep Agents by building a Claim Processing Agent — Applied AI #3

Source · Towards AI By Akshay Raman

From the publisher’s feed, collected in the ZeroLabs newsroom.

What Are Deep Agents?Some agent tasks are too big for one loop. The agent has to plan, read a lot of material, keep intermediate results somewhere, and not lose track along the way.If you’ve used Claude Code or Claude Cowork, you’ve seen this work: you hand over a broad objective, the agent plans it, executes, and adjusts the plan as it learns more.Source: https://www.langchain.com/blog/doubling-down-on-deepagentsDeep Agents is LangChain’s open-source library that brings this pattern to your own agents, on any model. It’s built on LangChain and runs on the LangGraph runtime, so everything you know about LangGraph still applies.LangChain calls it an agent harness: the usual tool-calling loop, plus built-in capabilities around it:A planning tool, so the agent breaks a task into steps and tracks its progress.A virtual file system, so the agent can offload large documents and intermediate results instead of keeping everything in context.Subagents, so isolated tasks (like reading one receipt) run in their own context and return only the result.Long-term memory, so knowledge can persist across runs through a pluggable storage backend.These sound abstract until you hit the problem they solve. So let’s hit it.What we’ll be buildingEvery company has an expense reimbursement process. Employees pay out of their pocket on work-related things and submit an expense claims to get it back. Typically this includes: Business Travel, Lodging, Meals, Office Supplies, Courses, Certifications etc.Each claim comes with supporting documents such as receipts, invoices and tickets. HR or Finance reviews every claim against these documents and decide an action.Example FlowWe’ll build an agent that does that review. It has to:Read receipts, extract the data, and cross-check it against the claim.Handle claims with many documents, such as a three-day business trip with daily meals, cabs, and hotel stays, without overflowing its context window.Decide how to group the data, run the calculations, and produce a report for a human reviewer.Each requirement maps to one of the capabilities above. Requirement 1 is a job for subagents, requirement 2 for the file system, and requirement 3 for planning. With plain LangChain and LangGraph, you’d hand-build all of that plumbing before writing a line of claims logic. Here it comes with the library.By the end, you’ll have a working agent that reads receipts, investigates each claim, and writes a report for a human reviewer.Let’s build it.Building the AgentFor this post I created sample receipts, claim requests, claim history, and a company policy for a fictional company, Acme Inc.Link to the Github repo — https://github.com/akshay-r13/claims-processing-deep-agentSample Policy DocumentSample Claim Requests — tied to employee id, dates and attendeesSample Receipts for a business tripFollowing is the repo layout:claims-processing-deep-agent/├── config/paths.py # DATA_DIR, CLAIM_REQUESTS_LIST├── data/│ ├── claim_policy_docs/acme_expense_policy.md│ ├── claim_requests/claim_requests.csv # new claim requests│ ├── employees/employees.csv│ ├── claim_history/reimbursed_claims.csv│ └── claim_requests/submitted_documents/CLM-1001/*.pdf├── agent/│ ├── __init__.py│ ├── tools.py│ ├── subagents.py│ ├── agent.py├── process_claim.py└── .envInstall the dependencies and set your Gemini key:pip install deepagents langchain-google-genai pdfplumber python-dotenv.envGOOGLE_API_KEY=your-keyCLAIMS_MODEL=google_genai:gemini-3.5-flashThe agent has four parts: tools, a subagent, the main agent, and a runner.Step 1: Create our custom toolsDeep Agents come with default file tools (ls, read_file, write_file, glob, grep) that work on text. Our receipts are PDFs, and the agent can’t run code, so we add two tools.read_pdf extracts a receipt’s text. calculate is a restricted arithmetic evaluator. Models make arithmetic mistakes on tax, tip, and caps, so every sum goes through this tool.import astimport operatorfrom pathlib import Pathfrom config.paths import DATA_DIRimport pdfplumberfrom langchain_core.tools import tool@tooldef read_pdf(path: str) -> str: """Extract the text of one PDF under /data e.g. /data/claims/CLM-1004/receipts/meal_2026-10-14_dinner.pdf. Returns plain text.""" try: full = resolve_data_path(path) except ValueError as e: return f"ERROR: {e}" if not full.is_file() or full.suffix.lower() != ".pdf": return f"ERROR: not a PDF file: {path}" with pdfplumber.open(full) as pdf: text = "\n".join(page.extract_text() or "" for page in pdf.pages) return text.strip() or "ERROR: no extractable text (scanned image? needs OCR or a vision model)"@tooldef calculate(expression: str) -> str: """Evaluate a python arithmetic expression. Supports + - * / parentheses and min(), max(), round(). Use it for EVERY sum, percentage, cap and comparison; never do arithmetic yourself. Example: min(302.40, 6 * 50)""" try: result = eval(ast.parse(expression, mode="eval").body) except Exception as e: return f"ERROR: {e}" return f"{round(result, 2):.2f}"def resolve_data_path(virtual_path: str) -> Path: """Map agent path under /data/ to real file. Reject anything that escapes the data directory.""" rel = virtual_path.removeprefix("/data").lstrip("/") full = (DATA_DIR / rel).resolve() root = DATA_DIR.resolve() if full != root and root not in full.parents: raise ValueError(f"Path outside data directory: {virtual_path}") return fulldef eval(node): SUPPORTED_OPERATIONS = {ast.Add: operator.add, ast.Sub: operator.sub, ast.Mult: operator.mul, ast.Div: operator.truediv} SUPPORTED_FUNCTIONS = {"min": min, "max": max, "round": round} # Plain numeric value, base condition if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)): return node.value # + , - , x and / operations if isinstance(node, ast.BinOp) and type(node.op) in SUPPORTED_OPERATIONS: return SUPPORTED_OPERATIONS[type(node.op)](eval(node.left), eval(node.right)) # negative inversion if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub): return -eval(node.operand) # functions min max and round if (isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id in SUPPORTED_FUNCTIONS and not node.keywords): return SUPPORTED_FUNCTIONS[node.func.id](*[eval(a) for a in node.args]) raise ValueError("unsupported expression")calculate is not a raw eval. It only accepts numbers, four operators, and min, max, and round. resolve_data_path keeps the agent inside the data folder.Step 2: The receipt-reader subagentThis step solves the context-window problem. A 3-day trip has 14 receipts, and putting all their raw text into one agent’s context would bury it. Instead, each receipt goes to a subagent that reads it in an isolated context, saves the extracted fields as JSON to the file system, and returns a single line.from .tools import calculate, read_pdfRECEIPT_READER = { "name": "receipt-reader", "description": ( "Reads ONE receipt PDF and saves its extracted fields as JSON. " "Give it the receipt path and the output path. " "It returns a one-line summary, not the receipt text." ), "system_prompt": """You extract data from one receipt PDF. 1. Call read_pdf on the path you are given. 2. Build a JSON object with: file, vendor, receipt_no, date, line_items (list of {description, amount}), subtotal, tax, tip, total, payment_method, notes. Put anything unusual in notes (mismatched totals, 'copy' markers, missing fields). Use null for absent fields. Do not invent values. 3. Use calculate to check that line items, tax and tip add up to the total. Record any mismatch in notes. 4. Save the JSON with write_file to the output path you were given. 5. Reply with ONE line: vendor | date | total | output path. Never return the raw receipt text.""", "tools": [read_pdf, calculate],}A subagent is simply a dictionary with a name, a description, a system prompt, and its own tools. The main agent reads the description to decide when to delegate. The “reply with ONE line” rule is the important part, because a subagent that returned everything it read would defeat the purpose.Step 3: The main agentThe main agent needs a file backend, a system prompt, and the wiring.Here is the design choice that matters most. A CompositeBackend routes by path: anything under /data/ and /reports/goes to a real folder on disk, and everything else (/work) goes to an in-memory state backend. The agent’s scratch files never touch our repo.import osfrom deepagents import create_deep_agentfrom deepagents.backends import CompositeBackend, FilesystemBackend, StateBackendfrom .subagents import RECEIPT_READERfrom .tools import calculate, read_pdffrom config.paths import DATA_DIR, OUTPUT_DIRfrom langchain.chat_models import init_chat_modelMODEL = init_chat_model(os.environ["CLAIMS_MODEL"], include_thoughts=True)SYSTEM_PROMPT = """You are an expense claims reviewer at Acme Inc. You review one claim at a timeagainst the company policy and its receipts, then decide what happens next.Files:- /data is READ-ONLY reference data: the policy (markdown), employees.csv, the reimbursement history CSV, and the receipt PDFs for each claim. Use ls and glob to find what you need.- /work is your scratch space. /reports is where the final report goes. Write only there.Workflow:1. Make a plan with write_todos before doing anything else.2. Read the policy in full. Look up the employee and any prior reimbursements in the CSVs.3. List the claim's receipt PDFs. Delegate each receipt to the receipt-reader subagent, one receipt per task call, and run them in parallel. Each saves its fields to /work//extracted/.json.4. Read the extracted JSON files. Cross-check them against the claim form: totals, dates, attendees, travel request. Check for duplicates against the history and within the claim.5. Apply the policy category by category (by day for trips). Use the calculate tool for every sum, cap and comparison. Log all key calculations under /work//calc/calculations.json6. Decide: approve (in full or adjusted), reject, or request_info. If information needed for a decision is missing, request it. Never guess. Cite policy section numbers for every deduction or rejection. Flag claims that need sign-off.7. Write /reports/.md for the human reviewer, starting with these two lines: DECISION: APPROVED_AMOUNT: Then: a summary, a table of amounts (claimed, deducted, approved), findings with policy references, and follow-ups (sign-off needed, questions for the employee).Finish with a one-line summary of the decision."""def build_agent(): backend = CompositeBackend( default=StateBackend(), routes={"/data/": FilesystemBackend(root_dir=DATA_DIR, virtual_mode=True), "/reports/": FilesystemBackend(root_dir=OUTPUT_DIR, virtual_mode=True)} ) return create_deep_agent( model=MODEL, system_prompt=SYSTEM_PROMPT, tools=[read_pdf, calculate], subagents=[RECEIPT_READER], backend=backend )You don’t have to teach the model how to use planning, files, or subagents. Deep Agents already includes that guidance in its built-in system prompt, and ours is appended to it. Ours only says what the job is and what the steps are.The prompt also contains a rule that isn’t enforced: “/data is READ-ONLY”. It’s an instruction, not a permission. I come back to this at the end.Step 4: Running a claimThe claim form (employee, amount claimed, attendees, travel request ID) arrives as the request, the way a portal would send it. The script looks up the claim by ID and hands it to the agent:from dotenv import load_dotenvload_dotenv() # must run before the agent module reads CLAIMS_MODELimport argparseimport csvfrom agent.claims_agent import claims_agentfrom config.paths import CLAIM_REQUESTS_LISTparser = argparse.ArgumentParser()parser.add_argument("claim_id")def load_claim(claim_id): with open(CLAIM_REQUESTS_LIST, newline="") as f: for row in csv.DictReader(f): if row["claim_id"] == claim_id: return row raise SystemExit(f"Unknown claim: {claim_id}")if __name__ == "__main__": args = parser.parse_args() claims_agent.process_claim(load_claim(args.claim_id))And the wrapper that builds the prompt and invokes the agent:from .agent import build_agentclass ClaimsAgent: def __init__(self): self.agent = build_agent() def process_claim(self, claim: dict): details = "\n".join(f"{k} - {v or '(none)'}" for k, v in claim.items()) prompt = f"Review this expense claim.\n---\n{details}\n---" result = self.agent.invoke({"messages": [{"role": "user", "content": prompt}]}) return resultclaims_agent = ClaimsAgent()Watching it workI’ll use a business trip claim request because it’s the hardest case.It has 14 receipts, a hotel booking with charges that aren’t reimbursable, a meal day over the cap, and a claim total that doesn’t match the receipts.Here is what to look for, and what the correct answer is:It planned first. The todo list appears before any file is read, and items move from pending to done as the run progresses.It fanned out. Fourteen receipts were read in separate contexts. The main agent’s own context holds a plan, the policy, and short summaries.It caught a mismatch. The employee claimed $1,417.62, but the receipts total $1,416.72. A deli receipt says $18.90 and was entered as $19.80.It applied the policy by day.Company policy is referenced and the minibar and pay-per-view movie on the hotel folio are not reimbursable.It made the right call. Approve $1,346.93, flagged for Finance Director sign-off because the claim is over $1,000 (§2.2).Final Report generated by the AgentAcross all six claims:Results come out as expected.What Deep Agents did for usBack to the three requirements from the start:Read receipts and cross-check against the claim. The receipt-reader subagent handles extraction, and the main agent compares the results against the claim form and the history.Handle many documents without overflowing the context window. Subagents isolate the raw text, and the file system holds the extracted data. The main agent never sees a raw receipt.Group the data, calculate, and write a report. The plan broke the trip into days, calculate did every sum, and the agent wrote the report for a human.We wrote about 150 lines of agent code, two tools, one subagent, and a prompt. Planning, context offloading, and subagent orchestration came from the library.What you’d need to change before productionScanned receipts. read_pdf only works on PDFs with a text layer. Real receipts are often photos, so the subagent would need OCR or a vision model.Read-only is only a prompt. Nothing stops the agent from writing to /data. Deep Agents has filesystem permissions for this, and a real deployment should use them.Judgment varies between runs. The model decides which policy section applies, and it can be wrong or inconsistent. Run each claim several times and compare the decisions against a known answer key before trusting it. [Link to the next post in the series.]Memory. I didn’t use long-term memory here. It could hold things like per-employee patterns across claims, but that needs a persistent backend and a decision about what’s safe to remember.And that’s about itThank you for making it to the end! As an AI Engineer I find most AI articles a bit too theoretical. I started writing these pieces to share practical, hands-on techniques you can actually apply in your day-to-day work, focusing on one concept with a working example, and code you can run yourself.Follow me for more updates 🙂, and check out the Github repository here:https://github.com/akshay-r13/claims-processing-deep-agentLearning Deep Agents by building a Claim Processing Agent — Applied AI #3 was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.