TERA is a Hybrid Token-Efficient Routing Agent built for the AMD ACT II Hackathon (Track 1). It dynamically routes prompts using a three-tier architecture to minimize token consumption while maintaining output accuracy and protecting against container execution timeouts.
The execution pipeline runs in 6 distinct phases:
-
Centralized Configuration (
config.py) Loads and validates environment variables (FIREWORKS_API_KEY,FIREWORKS_BASE_URL, andALLOWED_MODELS) at startup. Enforces strict presence checks. -
Model Capability Ranking & Selection (
agent/model_selector.py) Inspects names inALLOWED_MODELSand ranks them from smallest (cheapest) to largest (most capable) using parameter-size heuristic parsing (e.g. matching26b,k2p7,m3, etc.).- Code-Specialized Priority: If a task requires code generation or debugging, the selector checks for and routes to a model containing
codeorcoderin its name (e.g.kimi-k2p7-code). - General Selection: Resolves the optimal cheap fallback model (
gemma-4-26b-a4b-it) and flagship reasoning model (minimax-m3) dynamically.
- Code-Specialized Priority: If a task requires code generation or debugging, the selector checks for and routes to a model containing
-
Dynamic Time-Budgeted Three-Tier Routing (
main.py&agent/router.py) Classifies tasks into 8 required categories using local keyword heuristics. To maximize token efficiency, it dynamically manages execution time against the 60-second limit:- Tier 1 (Local Gemma - 0 Tokens): Simple/moderate tasks (Sentiment, NER, Summarization, General) are resolved locally on the pre-baked Gemma model, incurring 0 Fireworks tokens, provided the remaining time budget is sufficient.
- Tier 2 (Cheap Remote - Low Tokens): Fallback to the cheap remote model (
gemma-4-26b-a4b-it) if the time budget runs low, avoiding container timeouts. - Tier 3 (Capable Remote - Higher Tokens): Hard math, logic, and code tasks route directly to flagship remote models (
minimax-m3orkimi-k2p7-code) for maximum accuracy.
-
API Execution (
agent/fireworks_client.py) Wraps HTTP POST requests to the Fireworks completions endpoint.- Context Compression: Integrates the
headroomlibrary to intelligently compress input prompt contexts to minimize remote prompt tokens. - Robust Retries: Handles transient network and API errors (HTTP 429, 502, 503, 504) using exponential backoff retry cycles.
- Context Compression: Integrates the
-
Deterministic Formatting (
agent/formatter.py) Normalizes output responses depending on task types (extracting JSON entities for NER, stripping markdown code fences, and normalizing math expressions). -
Log & Export Output (
main.py) Calculates pipeline execution stats, logs token metrics, and writes the formatted predictions to the output file.
Create a .env file in the root directory (based on .env.example).
FIREWORKS_API_KEY=your_fireworks_api_key_here
FIREWORKS_BASE_URL=https://api.fireworks.ai/inference/v1
ALLOWED_MODELS=accounts/fireworks/models/gemma-4-26b-a4b-it,accounts/fireworks/models/minimax-m3,accounts/fireworks/models/kimi-k2p7-code(Note: .env is ignored by Git and Docker to prevent credentials from leaking into repository check-ins or built images).
To test the pipeline on the host machine using a Python virtual environment:
- Ensure your python virtual environment is initialized:
python3 -m venv .venv .venv/bin/pip install -r requirements.txt
- Run the automated test script:
This will run
./test.sh
main.pyagainst the tasks listed ininput/tasks.json, write results tooutput/results.json, and print a token consumption summary.
To run the automated pytest test suite:
.venv/bin/pytestTo package the agent into a lightweight or standard GGUF-bundled Docker container:
- Build the image:
docker build -t tera-agent . - Run the container:
docker run --rm \ -v $(pwd)/input:/input \ -v $(pwd)/output:/output \ -e FIREWORKS_API_KEY="your_api_key" \ -e FIREWORKS_BASE_URL="https://api.fireworks.ai/inference/v1" \ -e ALLOWED_MODELS="accounts/fireworks/models/gemma-4-26b-a4b-it,accounts/fireworks/models/minimax-m3,accounts/fireworks/models/kimi-k2p7-code" \ tera-agent
We evaluated TERA locally on a mock dataset containing one task for each of the 8 capability categories.
- Models:
gemma-4-26b-a4b-it(Cheap fallback),minimax-m3(Capable logic/math),kimi-k2p7-code(Capable code) - Local Model:
google_gemma-4-E4B-it-Q4_K_M.gguf(Tier 1 Local) - Total Elapsed Latency: 34.4s (~4.3s average per task)
| Task ID | Task Category | Routing Tier / Model | Status | Fireworks Prompt Tokens | Fireworks Completion Tokens |
|---|---|---|---|---|---|
t-general |
General | Tier 1 (Local Gemma) | Success | 0 | 0 |
t-math |
Math | Tier 3 (minimax-m3) | Success | 360 | 109 |
t-sentiment |
Sentiment | Tier 1 (Local Gemma) | Success | 0 | 0 |
t-summary |
Summary | Tier 1 (Local Gemma) | Success | 0 | 0 |
t-ner |
NER | Tier 1 (Local Gemma) | Success | 0 | 0 |
t-codedebug |
Code Debug | Tier 3 (kimi-k2p7-code) | Success | 128 | 80 |
t-codegen |
Code Gen | Tier 3 (kimi-k2p7-code) | Success | 112 | 245 |
t-logic |
Logic | Tier 3 (minimax-m3) | Success | 348 | 95 |
- Prompt Tokens: 948
- Completion Tokens: 529
- Total Token Score: 1477 (down from 2417 tokens in Category-based routing!)