vinkius-labs / synapse-toon
Synapse TOON - high-performance API payload optimization and streaming toolkit for Laravel 11/12
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/vinkius-labs/synapse-toon
Requires
- php: ^8.2 || ^8.3
- illuminate/support: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- laravel/pint: ^1.25
- mockery/mockery: ^1.6
- orchestra/testbench: ^10.6.0
- phpunit/phpunit: ^10.5 || ^11.0
- squizlabs/php_codesniffer: ^3.6
README
Synapse TOON is a Laravel-native engine for extreme API payload optimization and LLM cost reduction. It transforms verbose JSON into ultra-dense representations, trimming token consumption by 25β45% while preserving full semantic fidelityβso you ship faster APIs and pay dramatically less for inference.
Every runtime surface ships with an explicit SynapseToon prefix, making package ownership obvious in your codebase and eliminating class-name collisions.
"When every token costs money, compression becomes product strategy." β The Synapse TOON Manifesto π°
Why Synapse TOON?
- Cost Savings First β Consistently reduce LLM API bills by 25β45% through entropy-aware encoding, adaptive compression, and smart routing.
- Performance Native β HTTP/3 detection, Brotli/Gzip negotiation, and the
SynapseToonSseStreamerdeliver sub-100β―ms response times without manual tuning. - Observable by Default β
SynapseToonLogMetricsDriver, Prometheus, and Datadog integrations expose savings metrics, thresholds, and ROI in real time. - Production Ready β Queue-aware
SynapseToonProcessLLMBatchJob, SynapseToonEdgeCache, and complexity-aware routing keep high-traffic APIs responsive. - Framework Native β Middleware aliases, response macros, and Octane preloading mean zero friction for Laravel teams.
- Zero Lock-in β Bring your own vector stores, LLM clients, and cache driversβthe contracts stay feather-light yet explicit.
Table of Contents
- Getting Started
- Configuration Guide
- Encoding & Compression
- Streaming & SSE
- Metrics & Analytics
- RAG Integration
- Batch Processing
- GraphQL Adapter
- Edge Cache
- HTTP/3 Optimization
- Cost Optimization Guide
- Performance Tuning
- Technical Reference
- Contributing
Quick Peek
π° Cost Reduction in Action
use VinkiusLabs\SynapseToon\Facades\SynapseToon; // Before: 1,247 tokens β After: 683 tokens (45.2% reduction) $encoded = SynapseToon::encoder()->encode([ 'products' => Product::with('category', 'reviews')->get(), 'meta' => ['page' => 1, 'per_page' => 50], ]); return response()->synapseToon($encoded);
π Real-Time Savings Analytics
SynapseToon::metrics()->record([ 'endpoint' => '/api/products', 'json_tokens' => 1247, 'toon_tokens' => 683, 'savings_percent' => 45.2, 'compression_algorithm' => 'brotli', ]);
π Streaming LLM Responses
return response()->synapseToonStream($llmStream, function ($chunk) { return [ 'delta' => $chunk['choices'][0]['delta']['content'], 'usage' => $chunk['usage'] ?? null, ]; });
π― Smart Model Routing
$target = SynapseToon::router()->route($payload, [ 'complexity' => 0.4, 'tokens' => 512, ]); SynapseToon::router()->send($payload, [ 'connection' => 'openai.client', 'batch' => true, ]);
π RAG Context Optimization
$context = SynapseToon::rag()->buildContext( 'How do I implement OAuth2 in Laravel?', ['user_id' => auth()->id()] );
π§ Edge Cache Warmups
$payload = SynapseToon::edgeCache()->remember('feeds:new', function () { return ProductResource::collection(Product::latest()->take(100)->get()); });
π§ͺ Batch Job Offloading
SynapseToonProcessLLMBatchJob::dispatch($prompts, [ 'queue' => 'llm-batch', 'connection' => 'openai', 'batch_size' => 50, ]);
What's Inside
SynapseToonEncoder/SynapseToonDecoderβ Lossless TOON codec with dictionary support, chunked encoding, and heuristics aware of entropy per token.SynapseToonCompressorβ Adaptive Brotli (Q8), Gzip, and Deflate selection based onAccept-Encodingand HTTP/3 hints.SynapseToonSseStreamerβ Server-Sent Events with zero-copy chunking, UUID event IDs, and buffer flush guardrails.SynapseToonEdgeCacheβ Encode-once edge cache helper tuned for Redis/Octane workloads.SynapseToonMetricsβ Driver-agnostic metrics core speaking toSynapseToonLogMetricsDriver, Prometheus, Datadog, or custom drivers.SynapseToonProcessLLMBatchJobβ Queue-friendly batch encoder that collapses up to 50 prompts per dispatch while tracking ROI.SynapseToonLLMRouterβ Complexity-aware model router with pluggableSynapseToonLLMClientimplementations.SynapseToonRagServiceβ Vector-store abstraction that squeezes context with snippet thresholds and metadata braiding.SynapseToonGraphQLAdapterβ Drops straight into Lighthouse or Rebing GraphQL pipelines with TOON encoding baked in.SynapseToonPayloadAnalyzerβ Token analytics and savings calculator powering middleware and dashboards.
π‘ Real-World Impact
| Scenario | Before | After | Savings |
|---|---|---|---|
| E-commerce product feed (500 items) | 47,200 tokens | 26,100 tokens | 44.7% |
| Chat completion with context | 3,840 tokens | 2,310 tokens | 39.8% |
| GraphQL nested query | 2,156 tokens | 1,405 tokens | 34.8% |
| RAG context injection | 1,920 tokens | 1,152 tokens | 40.0% |
| Batch job (50 prompts) | 12,500 tokens | 7,000 tokens | 44.0% |
Average token reduction: 40.7%
At $0.03/1K tokens: $600/month β $356/month = $244 saved monthly
π Installation
composer require vinkius-labs/synapse-toon
Publish configuration:
php artisan vendor:publish --tag=synapse-toon-config
Register middleware in bootstrap/app.php (Laravel 11+):
->withMiddleware(function (Middleware $middleware) { $middleware->api(append: [ \VinkiusLabs\SynapseToon\Http\Middleware\SynapseToonCompressionMiddleware::class, \VinkiusLabs\SynapseToon\Http\Middleware\SynapseToonHttp3Middleware::class, ]); })
π― Core Use Cases
1. LLM API Cost Reduction
Route::middleware(['synapsetoon.compression'])->group(function () { Route::post('/ai/complete', [AIController::class, 'complete']); Route::post('/ai/embed', [AIController::class, 'embed']); });
2. Real-Time Streaming
public function streamCompletion(Request $request) { $stream = OpenAI::chat()->createStreamed([ 'model' => 'gpt-4o', 'messages' => $request->input('messages'), 'stream' => true, ]); return response()->synapseToonStream($stream); }
3. Batch Processing
SynapseToonProcessLLMBatchJob::dispatch($prompts, [ 'queue' => 'llm-batch', 'connection' => 'openai', 'batch_size' => 50, ]);
4. RAG Optimization
$optimizedContext = SynapseToon::rag()->buildContext($userQuery, [ 'limit' => 3, 'max_snippet_length' => 200, ]);
βοΈ Configuration Highlights
'compression' => [ 'prefer' => 'brotli', 'brotli' => ['quality' => 8, 'mode' => 'generic'], ], 'metrics' => [ 'driver' => 'prometheus', 'thresholds' => ['minimum_savings_percent' => 8], ], 'batch' => [ 'size' => 50, 'delimiter' => "\t", ], 'router' => [ 'strategies' => [ ['name' => 'ultra-light', 'max_tokens' => 512, 'target' => 'gpt-4o-mini'], ['name' => 'balanced', 'max_tokens' => 2048, 'target' => 'gpt-4o'], ], 'default_target' => 'o1-preview', ],
π§ͺ Local Development
docker compose build
docker compose run --rm app bash -lc \
"composer install && vendor/bin/phpunit"
docker compose run --rm app bash
π Compatibility Matrix
| Component | Support |
|---|---|
| Laravel | 10.x, 11.x, 12.x |
| PHP | 8.2, 8.3 |
| Octane | Swoole, RoadRunner, FrankenPHP |
| HTTP/3 | Full detection & optimization |
| Brotli | Requires ext-brotli (optional) |
π― Performance Benchmarks
Encoding Speed:
- 1KB payload: ~0.12ms
- 10KB payload: ~0.87ms
- 100KB payload: ~8.4ms
Compression (Brotli quality 8):
- 10KB β 2.3KB (77% reduction) in ~2.1ms
- 100KB β 18.7KB (81% reduction) in ~19.3ms
Total Overhead:
- Small responses (<5KB): +0.5-1.2ms
- Medium responses (5-50KB): +1.5-4.8ms
- Large responses (50KB+): +8-25ms
ROI Break-Even:
- Token cost savings > overhead at ~200 tokens
- Average API response: 1,500 tokens
- Net savings: ~40% cost reduction