Otimização e Benchmarking

Métricas em tempo real, batching adaptativo, estimativa de custos, aprendizado contínuo de cada tradução.

Continuous Optimization Loop

Every translation teaches the system. The middleware observes, the SDK applies guardrails, the benchmark harness measures, and the insights endpoint feeds it all back.

📊 Real-Time Metrics

  • • /insights: p50/p95 latency
  • • Throughput (chars/sec)
  • • Error rate & top errors
  • • Repeat text detection
  • • Queue depth & workers

🎯 Adaptive Batching

  • • SDK: BATCH_ITEM_LIMIT (default 45)
  • • SDK: BATCH_CHAR_LIMIT (default 45k)
  • • SDK: THROTTLE_MS between requests
  • • SDK: CHECKPOINT_EVERY N batches
  • • SDK: RUN_CHAR_LIMIT abort threshold

💰 Cost Estimation

  • • Benchmark: baseline chars/sec per pair
  • • ai_cost.py: LT vs AI token cost
  • • x402 hook for pay-per-request
  • • Report: JSON + Markdown
  • • Regression gate in CI

🧠 Learning Loop

  • • X-Btoven-Advisory headers (invisible to dumb clients)
  • • ?advisory=1 wraps JSON for smart agents
  • • Codes: use-async-queue, batch-smaller
  • • cache-opportunity, slow-pair
  • • backpressure, low-throughput

Benchmarking — What We Measure

$ make bench           # quick profile (3 sizes × 4 pairs)
$ make bench-standard  # standard (all sizes × 6 pairs × docker-stats)
$ make bench-full      # full suite (incl. AI cost comparison)
$ make bench-jobs      # with async queue backend
$ make bench-tests     # test suite = benchmark data

Corpus: deterministic mixed content (plain, URLs, HTML, code). Runs against real LibreTranslate container with docker-stats sampling (CPU, RAM). Outputs JSON + Markdown to benchmarks/results/.

Current Baseline (dev machine, Argos, en→es)

550-850
chars/sec (en→es)
3-5x slower
es→en vs en→es
~2ms
repeat text (cache hit)
$1.92/M
LT amortized cost

Dogfooding build (77 msgs, 2080 chars, 10 batches): ~31s incremental, 1.3s Astro build — no PC freeze.

SDK Guardrails (Env Config)

# Translation batch limits
BT_TRANSLATE_BATCH_ITEM_LIMIT=45      # max items per request
BT_TRANSLATE_BATCH_CHAR_LIMIT=45000   # max chars per request
BT_TRANSLATE_THROTTLE_MS=0            # ms between requests
BT_TRANSLATE_CHECKPOINT_EVERY=0       # write checkpoint every N batches (0=off)
BT_TRANSLATE_RUN_CHAR_LIMIT=0         # abort after N chars total (0=unlimited)

# Dogfooding conservative settings (btoven/frontend/astro.config.mjs):
BT_TRANSLATE_BATCH_ITEM_LIMIT=8
BT_TRANSLATE_BATCH_CHAR_LIMIT=8000
BT_TRANSLATE_THROTTLE_MS=750
BT_TRANSLATE_CHECKPOINT_EVERY=2

Debug / Advisory Channel — How Agents Learn

Set LT_ADVISORY_MODE=header (default) or embed. Every response gets X-Btoven-Advisory(base64 JSON) with actionable codes. Smart agents read this and self-optimize.

# Example advisory payload (decoded):
{
  "advisories": [
    {"code": "use-async-queue", "message": "6000 chars in one sync request. POST /jobs/translate instead..."},
    {"code": "cache-opportunity", "message": "Identical text seen 3x. Cache translations client-side...", "repeats": 3},
    {"code": "slow-pair", "message": "es→en runs 3-5x slower...", "pair": "es>en"}
  ],
  "hint": "chars=6000, items=1"
}

Query GET /insights for aggregated learning: throughput, latency distribution, error patterns, repeat text stats — the raw material for continuous improvement.

🔬 ANON-001: Anonymize Benchmark (Planned)

Measure overhead & utility of PII anonymization (52-pattern Protector from MCP):

  • • Flag --anonymize in scripts/benchmark/benchmark.py routes corpus through Protector
  • • Metrics: latency overhead, placeholder survival rate, translation quality delta
  • • Decision: is anonymize default ON or opt-in? (Current: opt-in via anonymize: true in payload)