InsightLoop.
A question. A query. A trace you can follow.
A Next.js data agent explores a synthetic retail warehouse in BigQuery. A hand-written loop chooses tools, guards read-only SQL, retries bounded failures and streams its investigation trace to the interface.
01 / THE PROBLEM
A result needs a trail.
A data question may need schema discovery, a query, error recovery or a clarification. InsightLoop exposes that process instead of showing a final answer with no path back to the underlying data.
02 / ARCHITECTURE
How the system moves.
- 01
Ask
The Next.js chat route accepts a question and opens an NDJSON response stream.
- 02
Plan
The custom loop sends the current state to a Gemini or Groq adapter with a common tool schema.
- 03
Choose a tool
The agent can inspect schema, run SQL or ask a clarifying question.
- 04
Constrain BigQuery
The SQL client permits one read-only SELECT/WITH statement, caps returned rows and cancels timed-out jobs.
- 05
Observe
Results or errors feed the next bounded step, while the UI shows trace events, SQL and the final answer.
03 / ENGINEERING DECISIONS
Why these boundaries matter.
Explicit loop
The application owns the plan, tool, observe and retry cycle rather than hiding it inside a framework.
Query boundary
The BigQuery client rejects mutating statements and applies a 15-second timeout with job cancellation.
Visible reasoning
Step and completion events drive the trace panel, so the user can inspect the query path.
04 / SOURCE EVIDENCE
What the repository shows.
- Custom loop bounded to nine tool rounds and three SQL retries
- Only a single SELECT or WITH query passes the SQL guard
- Streamed steps and a final answer over NDJSON
Selected files from the public repository, pinned to the revision reviewed for this case study.
EXPLORE THE REPOSITORYHONEST BOUNDARY
The public source supports the loop, guards and UI trace. This review did not independently run BigQuery, establish answer accuracy or verify production usage and business outcomes.