# arming-afk/hybrid-token-router architecture

> Architecture diagram of the GitHub repository arming-afk/hybrid-token-router, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/arming-afk/hybrid-token-router
- Repository: https://github.com/arming-afk/hybrid-token-router

## Overview

1. **Overall shape**
   - This is a Python batch inference service for the Fireworks AI judging/proxy environment.
   - The core path is: read tasks from an input JSON file, route each task to a category, choose a model tier, build a prompt, call Fireworks concurrently, then write `results.json`.

2. **Process boundary and I/O**
   - `src/main.py` is the runtime entrypoint.
   - It reads `/input/tasks.json` (or a path from env/tests) and must always emit a valid `/output/results.json`, even on partial failure.
   - This makes the app a file-in/file-out worker rather than a server.

3. **Task routing subsystem**
   - `src/router.py` classifies each task into one of 8 categories using regex-based local heuristics.
   - Only ambiguous inputs fall back to a small LLM-based classification, which is intentionally minimized to reduce token spend.
   - The router is a key architectural boundary because it decides the downstream prompt/model path before any expensive API call.

4. **Model tiering subsystem**
   - `src/models.py` takes `ALLOWED_MODELS` from the environment and organizes them into SMALL / MEDIUM / LARGE tiers.
   - It parses model parameter counts and sorts available models by size.
   - This module separates “what models are allowed by the runtime” from “which model tier a task category should use.”

5. **Prompt construction subsystem**
   - `src/prompts.py` maps each category to a tier, a compact instruction, and a `max_tokens` budget.
   - This is the primary tuning surface for token efficiency and answer quality.
   - The interface is shared and stable: category in, messages plus token cap and tier out.

6. **External inference boundary**
   - `src/client.py` encapsulates all Fireworks API calls and routes them through `FIREWORKS_BASE_URL`.
   - The repository treats the proxy as mandatory; bypassing it would break scoring assumptions.
   - The client is the only place where network interaction with the LLM provider should occur.

7. **Execution flow inside the main worker**
   - `src/main.py` orchestrates the full pipeline: load tasks, route each task, render prompts, select a model, send calls concurrently, and aggregate outputs.
   - The concurrency is important because the harness enforces a deadline; the code is designed to finish well before the 10-minute kill limit.
   - The output format is kept valid regardless of errors so the harness can still consume the file.

8. **Supporting scripts and local simulation**
   - `scripts/run_local.sh` is the Docker-based harness simulation.
   - `scripts/eval_matrix.py` and `scripts/token_report.py` support offline evaluation and token accounting.
   - These scripts are operational tooling around the same main pipeline, not separate application logic.

9. **Test and evaluation structure**
   - `tests/` contains focused unit tests for client, main, models, prompts, and router.
   - `tests/router_cases.json` and the `tests/eval/*.json` files encode domain-specific evaluation sets for the 8 task categories.
   - `docs/eval-results.md` appears to track benchmark or tuning outcomes, which suggests an iteration loop centered on routing accuracy and token usage.

10. **Packaging and release**
   - `Dockerfile` packages the worker for the competition harness.
   - `.github/workflows/release.yml` indicates a CI/CD path for building or publishing submission artifacts.
   - The repo is designed to run identically in local Docker and in the external scoring environment.

11. **Architectural dependencies that matter**
   - Runtime is Python with dependencies from `requirements.txt`.
   - External service dependency is Fireworks AI, accessed via environment variables for key/base URL/model list.
   - The architecture is optimized around two constraints: accuracy under an LLM judge and token minimization at the proxy.

12. **What to draw in a diagram**
   - A single batch worker node at the center.
   - Input JSON file on the left, output JSON file on the right.
   - Inside the worker, show a routing stage, a tier-selection stage, a prompt-rendering stage, and an API client boundary to Fireworks AI.
   - Around it, show test/eval assets and Docker/CI as supporting build-and-validation layers.

Main entry points: [`src/main.py`](https://github.com/arming-afk/hybrid-token-router/blob/main/src/main.py) (Worker), [`scripts`](https://github.com/arming-afk/hybrid-token-router/tree/main/scripts) (Ops scripts), [`tests`](https://github.com/arming-afk/hybrid-token-router/tree/main/tests) (Tests), [`src/router.py`](https://github.com/arming-afk/hybrid-token-router/blob/main/src/router.py) (Router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime worker"]
  node_input_tasks["Input tasks<br/>json input<br/>[sample_tasks.json]"]
  node_main["Worker<br/>batch orchestrator<br/>[main.py]"]
  node_router["Router<br/>category classifier<br/>[router.py]"]
  node_models["Model tiers<br/>tier selector<br/>[models.py]"]
  node_prompts["Prompt map<br/>prompt builder<br/>[prompts.py]"]
  node_client["Fireworks client<br/>api boundary<br/>[client.py]"]
  node_results["Output results<br/>json output<br/>[sample_tasks.json]"]
  node_fireworks(("Fireworks AI<br/>llm service"))
end

subgraph group_support["Supporting tools"]
  node_scripts["Ops scripts<br/>tooling"]
  node_run_local["Local run<br/>docker harness<br/>[run_local.sh]"]
  node_eval_matrix["Eval matrix<br/>offline eval<br/>[eval_matrix.py]"]
  node_token_report["Token report<br/>analysis tool<br/>[token_report.py]"]
end

subgraph group_validation["Tests and eval"]
  node_tests["Tests<br/>unit and eval"]
  node_router_cases["Router cases<br/>eval corpus<br/>[router_cases.json]"]
  node_eval_sets["Eval sets<br/>benchmark corpus"]
  node_docs_eval["Eval notes<br/>results log<br/>[eval-results.md]"]
end

subgraph group_packaging["Build and release"]
  node_dockerfile["Dockerfile<br/>container build"]
  node_release["Release CI<br/>workflow<br/>[release.yml]"]
  node_requirements["Deps<br/>python requirements<br/>[requirements.txt]"]
end

node_input_tasks -->|"load"| node_main
node_main -->|"classify"| node_router
node_router -->|"tier hint"| node_models
node_router -->|"category"| node_prompts
node_models -->|"available tiers"| node_prompts
node_main -->|"invoke"| node_client
node_prompts -->|"messages"| node_client
node_client -->|"request"| node_fireworks
node_main -->|"write"| node_results
node_scripts -->|"simulate"| node_run_local
node_scripts -->|"evaluate"| node_eval_matrix
node_scripts -->|"measure"| node_token_report
node_tests -->|"validate"| node_router_cases
node_tests -->|"benchmark"| node_eval_sets
node_tests -->|"record"| node_docs_eval
node_dockerfile -->|"package"| node_main
node_release -->|"build"| node_dockerfile
node_requirements -->|"deps"| node_main

click node_input_tasks "https://github.com/arming-afk/hybrid-token-router/blob/main/tests/sample_tasks.json"
click node_main "https://github.com/arming-afk/hybrid-token-router/blob/main/src/main.py"
click node_router "https://github.com/arming-afk/hybrid-token-router/blob/main/src/router.py"
click node_models "https://github.com/arming-afk/hybrid-token-router/blob/main/src/models.py"
click node_prompts "https://github.com/arming-afk/hybrid-token-router/blob/main/src/prompts.py"
click node_client "https://github.com/arming-afk/hybrid-token-router/blob/main/src/client.py"
click node_results "https://github.com/arming-afk/hybrid-token-router/blob/main/tests/sample_tasks.json"
click node_scripts "https://github.com/arming-afk/hybrid-token-router/tree/main/scripts"
click node_run_local "https://github.com/arming-afk/hybrid-token-router/blob/main/scripts/run_local.sh"
click node_eval_matrix "https://github.com/arming-afk/hybrid-token-router/blob/main/scripts/eval_matrix.py"
click node_token_report "https://github.com/arming-afk/hybrid-token-router/blob/main/scripts/token_report.py"
click node_tests "https://github.com/arming-afk/hybrid-token-router/tree/main/tests"
click node_router_cases "https://github.com/arming-afk/hybrid-token-router/blob/main/tests/router_cases.json"
click node_eval_sets "https://github.com/arming-afk/hybrid-token-router/tree/main/tests/eval"
click node_docs_eval "https://github.com/arming-afk/hybrid-token-router/blob/main/docs/eval-results.md"
click node_dockerfile "https://github.com/arming-afk/hybrid-token-router/tree/main/Dockerfile"
click node_release "https://github.com/arming-afk/hybrid-token-router/blob/main/.github/workflows/release.yml"
click node_requirements "https://github.com/arming-afk/hybrid-token-router/blob/main/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_input_tasks,node_main,node_router,node_models,node_prompts,node_client,node_results,node_fireworks toneBlue
class node_scripts,node_run_local,node_eval_matrix,node_token_report toneAmber
class node_tests,node_router_cases,node_eval_sets,node_docs_eval toneMint
class node_dockerfile,node_release,node_requirements toneRose
```

## Components

### Runtime worker

Batch inference path that loads tasks, routes them, calls Fireworks, and writes results.

- [Input tasks](https://github.com/arming-afk/hybrid-token-router/blob/main/tests/sample_tasks.json): json input · Batch tasks read from /input/tasks.json or a test override path. · `tests/sample_tasks.json`
- [Worker](https://github.com/arming-afk/hybrid-token-router/blob/main/src/main.py): batch orchestrator · Entry point that loads tasks, routes each item, builds prompts, runs concurrent inference, and always writes results. · `src/main.py`
- [Router](https://github.com/arming-afk/hybrid-token-router/blob/main/src/router.py): category classifier · Regex-first task classifier that assigns one of the 8 categories with minimal LLM fallback. · `src/router.py`
- [Model tiers](https://github.com/arming-afk/hybrid-token-router/blob/main/src/models.py): tier selector · Parses allowed models and organizes them into SMALL, MEDIUM, and LARGE tiers. · `src/models.py`
- [Prompt map](https://github.com/arming-afk/hybrid-token-router/blob/main/src/prompts.py): prompt builder · Maps category to tier, instruction, and token budget; returns messages plus max_tokens. · `src/prompts.py`
- [Fireworks client](https://github.com/arming-afk/hybrid-token-router/blob/main/src/client.py): api boundary · Encapsulates all network calls to Fireworks through the proxy base URL. · `src/client.py`
- [Output results](https://github.com/arming-afk/hybrid-token-router/blob/main/tests/sample_tasks.json): json output · Valid results file emitted even when some tasks fail. · `tests/sample_tasks.json`
- **Fireworks AI**: llm service · External inference provider reached only through the client boundary.

### Supporting tools

Local simulation, token analysis, and offline evaluation helpers.

- [Ops scripts](https://github.com/arming-afk/hybrid-token-router/tree/main/scripts): tooling · Local harness simulation and offline evaluation/token reporting utilities. · `scripts`
- [Local run](https://github.com/arming-afk/hybrid-token-router/blob/main/scripts/run_local.sh): docker harness · Docker-based simulation of the competition runtime. · `scripts/run_local.sh`
- [Eval matrix](https://github.com/arming-afk/hybrid-token-router/blob/main/scripts/eval_matrix.py): offline eval · Runs evaluation sweeps across task sets and model choices. · `scripts/eval_matrix.py`
- [Token report](https://github.com/arming-afk/hybrid-token-router/blob/main/scripts/token_report.py): analysis tool · Reports token usage and helps tune prompt budgets. · `scripts/token_report.py`

### Tests and eval

Unit tests and category-specific evaluation sets used to verify routing and prompt behavior.

- [Tests](https://github.com/arming-afk/hybrid-token-router/tree/main/tests): unit and eval · Focused tests for the router, models, prompts, client, and main worker. · `tests`
- [Router cases](https://github.com/arming-afk/hybrid-token-router/blob/main/tests/router_cases.json): eval corpus · Domain-specific cases for validating category routing behavior. · `tests/router_cases.json`
- [Eval sets](https://github.com/arming-afk/hybrid-token-router/tree/main/tests/eval): benchmark corpus · Category-focused evaluation JSON files for codegen, factuality, logic, math, NER, sentiment, summarization, and debug. · `tests/eval`
- [Eval notes](https://github.com/arming-afk/hybrid-token-router/blob/main/docs/eval-results.md): results log · Tracks benchmark outcomes and tuning iterations. · `docs/eval-results.md`

### Build and release

Container and CI assets for running the worker in the competition harness.

- [Dockerfile](https://github.com/arming-afk/hybrid-token-router/tree/main/Dockerfile): container build · Packages the worker for the external scoring harness. · `Dockerfile`
- [Release CI](https://github.com/arming-afk/hybrid-token-router/blob/main/.github/workflows/release.yml): workflow · GitHub Actions workflow for building or publishing submission artifacts. · `.github/workflows/release.yml`
- [Deps](https://github.com/arming-afk/hybrid-token-router/blob/main/requirements.txt): python requirements · Pinned runtime dependencies for the worker. · `requirements.txt`

## Connections

- Input tasks → Worker: load · The worker reads batch tasks from the input JSON file.
- Worker → Router: classify · Each task is routed before any expensive model call.
- Router → Model tiers: tier hint · The selected category determines which model tier should be used.
- Router → Prompt map: category · Prompt templates are chosen by category.
- Model tiers → Prompt map: available tiers · Prompt construction consumes the tier mapping and max token guidance.
- Worker → Fireworks client: invoke · The orchestrator sends prepared requests to the Fireworks client.
- Prompt map → Fireworks client: messages · Rendered messages and token budgets feed the API call.
- Fireworks client → Fireworks AI: request · All LLM traffic goes through the Fireworks proxy boundary.
- Worker → Output results: write · The worker always emits a valid results file, even on partial failure.
- Ops scripts → Local run: simulate · Local Docker harness for reproducing the runtime.
- Ops scripts → Eval matrix: evaluate · Offline evaluation sweeps over task sets and models.
- Ops scripts → Token report: measure · Token accounting and prompt-efficiency analysis.
- Tests → Router cases: validate · Router-specific domain cases.
- Tests → Eval sets: benchmark · Evaluation corpora for the 8 task categories.
- Tests → Eval notes: record · Benchmark outcomes and tuning notes.
- Dockerfile → Worker: package · Container image packages the batch worker entrypoint.
- Release CI → Dockerfile: build · CI builds or publishes the containerized submission artifact.
- Deps → Worker: deps · Runtime dependency set used by the worker.
