Agent
A LangGraph agent that answers questions about spending, and proves every figure it says.
A LangGraph agent that answers questions about spending, and proves every figure it says.
The real graph, with a scripted language model. Choose how the model behaves.
agent/src/costmap_agent/graph/agent.pyg = StateGraph(State)
for name, fn in (("start", start), ("route", route), ("clarify", clarify), ("chat", chat), ("agent", agent), ("tools", run_tools),
("verify_numbers", verify_numbers), ("redraft", redraft), ("answer", answer)):
g.add_node(name, fn)
g.add_edge(START, "start")
g.add_edge("start", "route")
g.add_conditional_edges("route", lambda s: {"clarify": "clarify", "chat": "chat"}.get(s["route"], "agent"), ["clarify", "chat", "agent"])
g.add_edge("clarify", "route")
g.add_edge("chat", "verify_numbers")
g.add_conditional_edges("agent", after_agent, ["tools", "verify_numbers"])
g.add_edge("tools", "agent")
g.add_conditional_edges("verify_numbers", after_verify, ["answer", "redraft"])
g.add_conditional_edges("redraft", after_redraft, ["tools", "verify_numbers"])
g.add_edge("answer", END)
return g.compile(checkpointer=checkpointer) agent/src/costmap_agent/graph/tools.pyasync def mcp_tools(client: Any) -> list[BaseTool]:
"""One LangChain tool per MCP tool. `client` is a connected `mcp.Client`; the tool schemas come straight from the server."""
tools = []
for t in (await client.list_tools()).tools:
async def call(_name: str = t.name, **kwargs: Any) -> str:
res = await client.call_tool(_name, kwargs)
body = res.structured_content.get("result", res.structured_content) if res.structured_content is not None else None
if body is None: # the server sent the result as text: decode JSON when it is JSON
text = "".join(getattr(c, "text", "") for c in res.content)
try:
body = json.loads(text)
except ValueError:
body = {"text": text}
return json.dumps(body, ensure_ascii=False, default=str)
tools.append(StructuredTool(name=t.name, description=t.description or "", args_schema=t.input_schema, coroutine=call))
return tools | Tool | What it does |
|---|---|
search_purchases | Payments by shop, period, category |
spending_summary | Totals by category or shop |
price_history | One product over time |
compare_prices | One product across shops |
upcoming_subscriptions | Next payments due |
open_questions | What waits for the user |
get_receipt | One receipt |
search_documents | Retrieval over receipt text |
answer_question | Changes data: needs approval |
agent/src/costmap_agent/graph/agent.pyasync def verify_numbers(state: State) -> dict:
ev = collect_evidence([e["result"] for e in state.get("evidence", [])])
rep = verify(state.get("draft", ""), ev)
return {"verification": {"ok": rep.ok, "unverified": rep.unverified, "n_claims": len(rep.claims), "message": rep.message()}} 74 questions, scripted models that make mistakes on purpose. No real language model yet.
| Stand-in | Correct | Warned | Silent errors |
|---|---|---|---|
| Careful model | 100 % | 0 % | 0 |
| Model that invents a figure | 85 % | 15 % | 0 |
| Model that swaps two real figures | 63 % | 0 % | 23 |
Known weakness: a real figure put in the wrong place passes the check.