|
| 1 | +# gcf-python |
| 2 | + |
| 3 | +Python implementation of [GCF (Graph Compact Format)](https://github.com/blackwell-systems/gcf). |
| 4 | + |
| 5 | +**84% fewer tokens than JSON. 32% fewer than TOON. 100% LLM comprehension accuracy at 500 symbols, where JSON fails.** |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +``` |
| 10 | +pip install gcf-format |
| 11 | +``` |
| 12 | + |
| 13 | +Zero dependencies. Pure Python. Python 3.9+. |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +```python |
| 18 | +from gcf import encode, Payload, Symbol, Edge |
| 19 | + |
| 20 | +p = Payload( |
| 21 | + tool="context_for_task", |
| 22 | + token_budget=5000, |
| 23 | + tokens_used=1847, |
| 24 | + symbols=[ |
| 25 | + Symbol(qualified_name="pkg.AuthMiddleware", kind="function", score=0.78, provenance="lsp_resolved", distance=0), |
| 26 | + Symbol(qualified_name="pkg.NewServer", kind="function", score=0.54, provenance="lsp_resolved", distance=1), |
| 27 | + ], |
| 28 | + edges=[ |
| 29 | + Edge(source="pkg.NewServer", target="pkg.AuthMiddleware", edge_type="calls"), |
| 30 | + ], |
| 31 | +) |
| 32 | + |
| 33 | +output = encode(p) |
| 34 | +``` |
| 35 | + |
| 36 | +Output: |
| 37 | +``` |
| 38 | +GCF tool=context_for_task budget=5000 tokens=1847 symbols=2 |
| 39 | +## targets |
| 40 | +@0 fn pkg.AuthMiddleware 0.78 lsp_resolved |
| 41 | +## related |
| 42 | +@1 fn pkg.NewServer 0.54 lsp_resolved |
| 43 | +## edges |
| 44 | +@0<@1 calls |
| 45 | +``` |
| 46 | + |
| 47 | +## Decode |
| 48 | + |
| 49 | +```python |
| 50 | +from gcf import decode |
| 51 | + |
| 52 | +p = decode(input_text) |
| 53 | +print(p.tool, len(p.symbols), "symbols", len(p.edges), "edges") |
| 54 | +``` |
| 55 | + |
| 56 | +## Session Deduplication |
| 57 | + |
| 58 | +Track transmitted symbols across multiple tool responses. Previously-sent symbols become bare references instead of full declarations: |
| 59 | + |
| 60 | +```python |
| 61 | +from gcf import encode_with_session, Session, Payload, Symbol |
| 62 | + |
| 63 | +sess = Session() |
| 64 | + |
| 65 | +out1 = encode_with_session(payload1, sess) # full declarations |
| 66 | +out2 = encode_with_session(payload2, sess) # reused symbols as "@N # previously transmitted" |
| 67 | +``` |
| 68 | + |
| 69 | +By the 5th call in a session: 92.7% token savings vs JSON. |
| 70 | + |
| 71 | +## Delta Encoding |
| 72 | + |
| 73 | +When the consumer already has a prior context pack, send only what changed: |
| 74 | + |
| 75 | +```python |
| 76 | +from gcf import encode_delta, DeltaPayload, Symbol, Edge |
| 77 | + |
| 78 | +delta = DeltaPayload( |
| 79 | + tool="context_for_task", |
| 80 | + base_root="aaa111", |
| 81 | + new_root="bbb222", |
| 82 | + removed=[Symbol(qualified_name="pkg.OldFunc", kind="function")], |
| 83 | + added=[Symbol(qualified_name="pkg.NewFunc", kind="function", score=0.85, provenance="rwr")], |
| 84 | + delta_tokens=30, |
| 85 | + full_tokens=200, |
| 86 | +) |
| 87 | + |
| 88 | +output = encode_delta(delta) |
| 89 | +``` |
| 90 | + |
| 91 | +81.2% savings on re-queries where the pack changed slightly. |
| 92 | + |
| 93 | +## API |
| 94 | + |
| 95 | +| Function | Description | |
| 96 | +|----------|-------------| |
| 97 | +| `encode(p: Payload) -> str` | Encode a payload to GCF text | |
| 98 | +| `decode(input_text: str) -> Payload` | Parse GCF text back to a Payload | |
| 99 | +| `encode_with_session(p: Payload, s: Session) -> str` | Encode with session deduplication | |
| 100 | +| `encode_delta(d: DeltaPayload) -> str` | Encode a delta (added/removed only) | |
| 101 | +| `Session()` | Create a new session tracker (thread-safe) | |
| 102 | + |
| 103 | +## Types |
| 104 | + |
| 105 | +| Type | Purpose | |
| 106 | +|------|---------| |
| 107 | +| `Payload` | Full GCF payload: tool, budget, symbols, edges, pack root | |
| 108 | +| `Symbol` | Graph node: qualified name, kind, score, provenance, distance | |
| 109 | +| `Edge` | Directed relationship: source, target, edge type | |
| 110 | +| `DeltaPayload` | Diff between two packs: added/removed symbols and edges | |
| 111 | +| `Session` | Thread-safe tracker for multi-call deduplication | |
| 112 | +| `KIND_ABBREV` / `KIND_EXPAND` | Bidirectional kind abbreviation dicts | |
| 113 | + |
| 114 | +## Comprehension Eval |
| 115 | + |
| 116 | +Rigorous 3-way benchmark (GCF vs TOON vs JSON) at 500 symbols, 200 edges. Six structured extraction questions sent to an LLM: |
| 117 | + |
| 118 | +| Format | Accuracy | Tokens | vs JSON | |
| 119 | +|--------|----------|--------|---------| |
| 120 | +| **GCF** | **100%** (6/6) | **11,090** | **79% fewer** | |
| 121 | +| TOON | 100% (6/6) | 16,378 | 69% fewer | |
| 122 | +| JSON | 66.7% (4/6) | 53,341 | baseline | |
| 123 | + |
| 124 | +JSON failed on counting tasks. GCF and TOON both achieved perfect accuracy. GCF does it in 32% fewer tokens. |
| 125 | + |
| 126 | +## Token Efficiency (TOON's Own Benchmark) |
| 127 | + |
| 128 | +Running [TOON's benchmark harness](https://github.com/blackwell-systems/toon/tree/gcf-comparison) with GCF inserted (their datasets, their tokenizer): |
| 129 | + |
| 130 | +| Track | GCF | TOON | Result | |
| 131 | +|-------|-----|------|--------| |
| 132 | +| Mixed-structure (nested, semi-uniform) | 169,554 | 227,896 | **GCF 34% smaller** | |
| 133 | +| Flat-only (tabular) | 66,026 | 67,837 | **GCF 3% smaller** | |
| 134 | +| Semi-uniform event logs | 107,269 | 154,032 | **GCF 44% smaller** | |
| 135 | + |
| 136 | +GCF wins on every dataset except deeply nested config (75 tokens on a 618-token payload). On semi-uniform data, GCF uses 44% fewer tokens than TOON. |
| 137 | + |
| 138 | +Reproducible: [blackwell-systems/toon@gcf-comparison](https://github.com/blackwell-systems/toon/tree/gcf-comparison) |
| 139 | + |
| 140 | +## Other Implementations |
| 141 | + |
| 142 | +- **Go**: [github.com/blackwell-systems/gcf-go](https://github.com/blackwell-systems/gcf-go) |
| 143 | +- **TypeScript**: [github.com/blackwell-systems/gcf-typescript](https://github.com/blackwell-systems/gcf-typescript) |
| 144 | +- **Specification**: [github.com/blackwell-systems/gcf](https://github.com/blackwell-systems/gcf) |
| 145 | + |
| 146 | +## License |
| 147 | + |
| 148 | +MIT |
0 commit comments