Course 3: REST, SQL & N+1 Batching

Course 3 teaches federated query orchestration in GAP: diagnosing agent N+1 round-trip explosions, enabling declarative x-gap-in-param batching (INV-18), adding SQL & Code AST branches, and rolling out schemas safely via Propose-Not-Activate.

What you'll learn

  • Trace why an agent joining 50 customers without batching wastes 4 turns and 41,539+ tokens (up to 814,959 at N=1,000).
  • Declare OpenAPI "x-gap-in-param" batching (INV-18) and SQL parameterized IN (...) joins.
  • Walk through gap ingest once -> gap proposals list -> gap snapshot diff -> gap proposals approve.
On this page

Lesson 1: Spotting the Agent N+1 Waterfall

Course 3: REST, SQL & N+1 Batching · 25 min · 4 lessons 0 / 4 completed

When an LLM uses flat tools to fetch 50 customers and then retrieve orders or notes for each customer, the model re-reads the entire conversation transcript across 4 round-trips while emitting dozens of serialized HTTP requests. In GAP-Bench, at N=1,000 tools this consumes 814,959.8 tokens uncached vs. 8,547.9 tokens (-99.0%) in GAP:

LLM N+1 Waterfall vs GAP DataLoader BatchJoin (INV-18) Direct-MCP: N+1 Loop (4 turns · 814,959 tok) LLM calls customers -> receives 50 IDs Agent fires 50 sequential HTTP calls GAP @join: BatchJoin (2 turns · 8,547 tok) gap-exec deduplicates keys {1..50} ceil(unique_keys / max_batch) BatchQuery (INV-18)
LLM N+1 Waterfall vs GAP DataLoader BatchJoin (INV-18)

Key Takeaways

  • Flat agent orchestration turns relational joins into multi-turn prompt loops.
  • GAP deduplicates foreign keys inside gap-exec before touching upstream connectors.

Lesson 2: Declarative REST Batching with "x-gap-in-param" (INV-18)

Just like Apollo GraphOS REST Connectors batch keys with $batch, GAP's OpenAPI NGC adapter looks for "x-gap-in-param" on OpenAPI operations (verbatim from examples/walking-skeleton/shop.openapi.json):

json examples/walking-skeleton/shop.openapi.json
"/customers/{id}": {
  "get": {
    "operationId": "customer",
    "x-gap-in-param": {
      "path": "/customers",
      "param": "ids",
      "key_field": "id",
      "max_batch_size": 50
    }
  }
}
Code Challenge

Configure OpenAPI Key Batching

Fill in the exact OpenAPI vendor extension key checked by gap-ngc-openapi to collapse 50 individual lookups into bounded ceil(unique_keys / max_batch_size) calls (INV-18):

"operationId": "customer",
"____": {
  "path": "/customers",
  "param": "ids",
  "key_field": "id",
  "max_batch_size": 50
}
Quick hints:

Key Takeaways

  • INV-18 guarantees at most ceil(unique_keys / max_batch) RHS upstream calls per join edge.
  • No custom Rust glue code needed—just add "x-gap-in-param" in your OpenAPI JSON/YAML.

Lesson 3: Unifying SQL Databases & tree-sitter Code ASTs (208.7x)

Beyond REST, the same supergraph unifies relational databases (gap-ngc-sql using parameterized IN (...) clauses, never string concatenation) and source repositories (gap-ngc-code indexing callers, callees, and symbol definitions via tree-sitter):

toml Example Code AST Branch Config (TOML)
[[branches]]
id = "codebase"
protocol = "code"
trust = "T0"

[branches.code]
root = "."
languages = ["rust", "python", "typescript"]

On the code_graph benchmark (24 symbols), querying callers/callees through GAP replaces 11 noisy search-and-read agent turns (137,810.4 cached tokens) with 1 typed query (660.4 tokens — a 208.7x gain).

Key Takeaways

  • gap-ngc-sql projects foreign keys into batched joins using parameterized SQL IN clauses.
  • gap-ngc-code indexes repository AST graphs with tree-sitter for up to 208.7x code-understanding efficiency.

Lesson 4: Safe Schema Updates (Propose-Not-Activate & snapshot diff)

Neither schemas nor @join edges go live silently (INV-06). GAP runs sampled containment checks on proposed joins (never trusting LLM guesses) and requires operator inspection before atomic ArcSwap promotion:

terminal Terminal — Ingest, Diff & Approve Proposals Safely
./target/dist/gap ingest once --config examples/walking-skeleton/gap.toml --tenant default
./target/dist/gap proposals list
./target/dist/gap snapshot diff --before examples/walking-skeleton/gap.toml --after next.toml
./target/dist/gap proposals approve <proposal-id>
Knowledge Check

What happens if gap ingest discovers a candidate @join edge between two branches, but no operator has run gap proposals approve?

Key Takeaways

  • Only ACTIVE @join edges execute on Plane 1 (INV-06).
  • gap snapshot diff --before <old.toml> --after <new.toml> previews every schema delta cleanly.