惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
月光博客
月光博客
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
Vercel News
Vercel News
量子位
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
腾讯CDC
有赞技术团队
有赞技术团队

Hacker News: Show HN

PurrrrrFocus: Pomodoro Timer App - App Store Workflow Engine — Multi-Step Orchestration for Bun RapidPhoto: Pro Photo Editor App - App Store GitHub - DheerG/swarms: Achieve extraordinary results with claude code across a variety of tasks SPICE simulation → oscilloscope → verification with Claude Code — Lucas Gerads Show HN: VCoding – A 5 MB native Windows IDE with no dynamic dependencies Show HN: LLMs don't hallucinate because they're bad at math, it's the format GitHub - Agent-FM/agentfm-core: AgentFM is a peer-to-peer network that turns everyday computers into a decentralized AI supercomputer. AgentFM lets you run massive AI workloads directly across a global mesh of idle CPUs and GPUs. Show HN: Tracking Top US Science Olympiad Alumni over Last 25 Years GitHub - Potarix/agent-hub: One place to talk to all your agents Show HN: Runtime security for AI agents(injection,tool abuse, data exfiltration) GitHub - dubeyKartikay/lazyspotify: Terminal Spotify client for macOS and Linux GitHub - the-banana-tool/king-louie: Easy to use GUI Personal AI Assistant. Win/Linux/Mac. Show HN I made my vacation rental bookable by AI agents–no Airbnb, 0% commission GitHub - basteez/jsf-autoreload: maven plugin to enable hot reload on jsf projects uvm32/hosts/host-gdbstub at main · ringtailsoftware/uvm32 GitHub - labsai/EDDI: Config-driven engine that turns JSON into production-grade AI agents. Multi-agent orchestration, 12+ LLM providers, MCP/A2A protocols, RAG, persistent memory, and enterprise compliance (EU AI Act, GDPR, HIPAA). Built on Quarkus. GitHub - glitchnsec/fortyone-oss: AI Executive Assistant Platform Quickstart | Alien GitHub - muxshed/shed: One stream in, or many. Every destination, simultaneously. No cloud middleman, no per-channel fees, no limits. GitHub - ocrbase-hq/ocrbase: 📄 PDF/IMG ->.MD/JSON Document OCR API for PaddleOCR and GLMOCR. Self-hostable. GitHub - impactjo/home-memory: MCP server that lets your AI assistant remember everything about your home. GitHub - Sets88/dbcls: DbCls is a powerful terminal database client that supports various databases GitHub - neptun2000/heor-agent-mcp GitHub - SeanFDZ/macmind: Single-layer transformer in HyperTalk for the classic Macintosh RollQuation: Math Puzzles - Apps on Google Play GitHub - dropbox/witchcraft Show HN: Agent-cache – Multi-tier LLM/tool/session caching for Valkey and Redis GitHub - opentalon/opentalon: OpenTalon is an open-source platform built from the ground up in Go as a robust alternative to OpenClaw LinkedIn™ 职位抓取工具 - Chrome 应用商店
GitHub - MordechaiHadad/rustgate: A rust powered token-aw...
MordechaiHad · 2026-06-02 · via Hacker News: Show HN

Rustgate Backend

Small FastAPI backend that uses a Rust pyo3 extension for AI token-aware rate limiting backed by Redis.

Summary

This repository contains two parts:

  • bindings/ -- a Rust crate that exposes Python bindings via pyo3/maturin
  • backend/ -- a small FastAPI app that loads the compiled extension and serves a few endpoints

The Rust extension uses the axum-rate-limiter crate and OpenAI's tiktoken-rs to count query tokens. Rate limits are applied per model and per sliding window, using the query's estimated token count multiplied by a model-specific cost factor.

Prerequisites

  • Rust toolchain (rustc/cargo)
  • Python 3.10+
  • uv (the uv dependency manager)
  • Redis (running on default port 6379, or configure with RUSTGATE_REDIS_URL)

Quick Start

  1. Ensure uv is installed and available on PATH.

  2. Install dependencies and build everything via uv:

    uv sync

    "uv sync" installs pinned dependencies from uv.lock and runs the build steps for this repository, including building the Rust pyo3 extension and installing the local Python package into the environment uv manages.

Run the server

After uv sync completes you can start the FastAPI server with uv:

uv run uvicorn main:app --app-dir src --host 127.0.0.1 --port 8001

This uses the environment and commands declared in the repo's uv configuration.

Environment

  • RUSTGATE_REDIS_URL -- redis connection string used by the rate limiter (default: redis://127.0.0.1:6379/0)

API Endpoints

All POST endpoints accept a JSON body with a query field, parsed by the Rust layer for token counting.

  • GET /health -- basic health check, returns {"status": "ok"}

  • POST /models/auto -- tries gpt-5 first, falls back to gpt-4 if rate limited. Returns {"model": "<model_name>"}.

  • POST /models/gpt-5 -- attempts to use gpt-5. Returns 429 if rate limited.

  • POST /models/gpt-4 -- attempts to use gpt-4. Returns 429 if rate limited.

Rate Limiting

Rate limits are enforced in Rust via the RedisAiLimiter (axum-rate-limiter crate) with the following rules:

  • Sliding window: 10 minutes (600 seconds).
  • Total budget: 5000 charge units per window per client, identified by IP (X-Forwarded-For or remote address).
  • Per-token cost:
    • gpt-4 family: 1 charge unit per token
    • gpt-5 family: 25 charge units per token
  • Token counting: uses tiktoken-rs with the appropriate tokenizer (Cl100kBase for gpt-4, O200kBase for gpt-5).
  • Zero-token queries: bypass rate limiting entirely.

Example: a 200-token gpt-5 query costs 5000 charge units (200 x 25), consuming the entire budget. The same 200-token query against gpt-4 costs only 200 charge units (200 x 1).

Supported models

The Rust layer supports two model families:

  • gpt-4 and gpt-4.* (e.g. gpt-4, gpt-4.1)
  • gpt-5 and gpt-5.* (e.g. gpt-5, gpt-5.4)

Models like gpt-4o, gpt-4o-mini, gpt-5-mini, or o3 are not currently supported and will return a 400 error.

Benchmark (sample load test)

Load test with oha against the POST /models/auto endpoint:

oha -z 30s -c 100 -m POST -d '{"query": "This is my grand query"}' \
  http://localhost:8001/models/auto

Results:

Metric Value
Duration 30.01 s
Requests/sec 1128.10
Fastest latency 15.34 ms
Average latency 88.76 ms
Slowest latency 774.95 ms
p50 75.0 ms
p90 152.1 ms
p95 168.36 ms
p99 183.6 ms
Rate-limited (429) 33713
Successful (200) 40

Comparison with the lightweight GET /health endpoint (no rate limiting, no token counting, no model rerouting, just a fast return):

oha -z 30s -c 100 -m GET http://localhost:8001/health
Metric Value
Duration 30.00 s
Requests/sec 1496.39
Fastest latency 13.70 ms
Average latency 66.90 ms
Slowest latency 1.89 s
p50 51.10 ms
p90 58.80 ms
p95 73.40 ms
p99 656.30 ms
Successful (200) 44796