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
4turns and41,539+ tokens (up to814,959atN=1,000). - Declare OpenAPI
"x-gap-in-param"batching (INV-18) and SQL parameterizedIN (...)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
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:
Key Takeaways
- Flat agent orchestration turns relational joins into multi-turn prompt loops.
- GAP deduplicates foreign keys inside
gap-execbefore 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):
"/customers/{id}": {
"get": {
"operationId": "customer",
"x-gap-in-param": {
"path": "/customers",
"param": "ids",
"key_field": "id",
"max_batch_size": 50
}
}
}
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
}
Key Takeaways
INV-18guarantees at mostceil(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):
[[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-sqlprojects foreign keys into batched joins using parameterized SQLINclauses.gap-ngc-codeindexes repository AST graphs withtree-sitterfor 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:
./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>
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@joinedges execute on Plane 1 (INV-06). gap snapshot diff --before <old.toml> --after <new.toml>previews every schema delta cleanly.