chatbot-app
View on GitHub ↗
API & Agent

LangGraph Agent

--agent (CLI) and POST /agent + approve/reject (API) run a LangGraph StateGraph with three tools, behind a mandatory human-in-the-loop gate: no tool ever executes without explicit approval, in either mode.

The three tools

web_search
Searches the web through DuckDuckGo — no API key required.
calculator
Evaluates a restricted set of arithmetic expressions with an AST-based evaluator — no eval or exec.
read_doc
Reads supported files only from docs/ — path-confined, can't escape the corpus directory.

Graph & approval gate

agent/graph.py's AgentGraph compiles the graph with a MemorySaver checkpointer and interrupt_before=["tools"]: execution pauses right before a tool call would run. session_id doubles as the LangGraph thread_id — the same validated field used elsewhere, no new ID scheme.

CLI flow

Synchronous — when the graph pauses, it prints every proposed tool call (there can be more than one in a single turn, via parallel tool calling) and prompts once with [y/N]. Approval or rejection applies to the whole batch.

API flow

Two-step, by explicit design choice — an auto-approve-via-header alternative was considered and rejected as weakening the safety gate.

Neither approval route has a per-route rate-limit decorator, for the same ASGI-middleware reason as /chat//chat/rag — see REST API Reference.

Calculator cost bound

calculator's AST evaluator bounds ast.Pow's cost before computing, not just the grammar it accepts: Python ints are arbitrary-precision, so an innocuous-looking 9**9**9 would otherwise pin a CPU core computing a ~369-million-digit number. _check_pow_cost estimates the result's bit-length via log2 and rejects anything over _MAX_POW_RESULT_BITS before evaluating.

Audit log

Every tool-call attempt — approved-and-executed, or rejected — is logged as one JSON line to logs/agent.jsonl (gitignored, like sessions//chroma_db/): {"timestamp", "session_id", "tool", "args", "result", "declined", "approved_by"}. Tool results are truncated to 500 characters in the log only — the LLM/caller still receives the full result.

Known, accepted limitations — not bugs: MemorySaver is in-memory, so a pending approval is lost if the API process restarts before /approve//reject is called. verify_token authenticates one shared service secret, not a per-user identity, so any valid token holder can approve or reject any session's pending call — identical to the pre-existing /chat session_id trust model, not new to this feature.

Next steps