Imported from marcusyoung/optiproxai (
AGENTS.md). Install upstream withnpx skills add marcusyoung/optiproxai. Copyright stays with the author.
AGENTS.md
This file is for coding agents working in optiproxai.
Project Snapshot
- Language: Python 3.13+
- Package manager and task runner:
uv - Build backend:
uv_build - CLI entrypoints:
optiproxai = "optiproxai.cli:main"andopx = "optiproxai.cli:main" - App shape: Click CLI + FastAPI proxy + Pydantic config/models
- Source tree:
src/optiproxai/ - Tests:
tests/withpytest - Type checking:
pyright - Lint/format:
ruff
Repository Layout
src/optiproxai/cli.py- Click commands forserve,route,config,doctor,init, andkeyssrc/optiproxai/proxy.py- FastAPI OpenAI-compatible proxysrc/optiproxai/router.py- routing decisions, tier/profile/provider/model selection, tier overridesrc/optiproxai/scorer.py- distilled feature classification and tier scoringsrc/optiproxai/classification_context.py- request-to-classification context helperssrc/optiproxai/config.py- YAML config loading, env-var resolution, validationsrc/optiproxai/api_keys.py- proxy API key storage and validationsrc/optiproxai/fallback_backoff.py- process-local fallback cooldownssrc/optiproxai/logger.py- JSONL routing log writersrc/optiproxai/dirs.py- XDG-compliant directory pathssrc/optiproxai/dashboard.py- dashboard ingestion, stats, and HTML renderingsrc/optiproxai/tokens.py- token estimation (tiktoken with char/4 fallback)src/optiproxai/training_data.py- distilled feature dataset data structures/helperssrc/optiproxai/feature_training.py- multi-output feature classifier trainingsrc/optiproxai/agentic_training.py- deprecated wrappers for feature classifier trainingtests/test_scorer.py- distilled feature scorer and ambiguous band coveragetests/test_llm_classifier.py- routing logger coverage with distilled feature payloadstests/test_capability_routing.py- capability detection, filtering, and escalationtests/test_input_limit_routing.py- input-limit filtering and tier escalationtests/test_tier_override.py- per-turn tier override (parser, router, proxy, CLI)tests/test_fallback_backoff.py- fallback cooldown statetests/test_dashboard.py- dashboard ingestion, stats, and HTML renderingtests/test_proxy_reload.py- config hot reload, routing errors, decorative tool schematests/test_api_keys.py- proxy API key lifecycletests/test_api_keys_cli.py-keysCLI commandstests/test_api_keys_proxy.py- proxy auth and fallback behaviortests/test_router_logging.py- routing log and session-sticky routingtests/test_config.py- embedding config validationtests/test_cli.py- CLI route masking, config errors, doctor, init, aux LLM configtests/test_feature_training.py- feature classifier training pipelinetests/test_agentic_training_data.py- dataset extraction and LLM annotationtests/test_agentic_training_script.py- training script entry pointconfig.example.yaml- example configuration with all features documented
Setup Commands
- Install runtime + dev dependencies:
uv sync --dev - Install runtime dependencies only:
uv sync - Run the CLI locally:
uv run optiproxai --help - Start the proxy locally:
uv run optiproxai serve - Route a prompt locally:
uv run optiproxai route "hello world" - Show resolved config:
uv run optiproxai config
Build, Lint, Format, Typecheck, Test
- Build package artifacts:
uv build - Lint source:
uv run ruff check src/ - Check formatting:
uv run ruff format --check src/ tests/ - Auto-format source and tests:
uv run ruff format src/ tests/ - Type-check source:
uv run pyright src/ - Run full test suite:
uv run pytest tests/ -q
Single-Test Commands
- Run one test file:
uv run pytest tests/test_scorer.py -q - Run one test class:
uv run pytest tests/test_scorer.py::TestAmbiguousBands -q - Run one test method:
uv run pytest tests/test_scorer.py::TestAmbiguousBands::test_prefer_upper_fails_toward_higher_tier -q - Run tests matching an expression:
uv run pytest tests/ -q -k reasoning - Stop after first failure:
uv run pytest tests/ -q -x
CI Expectations
The GitHub Actions workflow in .github/workflows/ci.yml effectively defines the acceptance bar:
uv sync --devuv run ruff check src/uv run ruff format --check src/ tests/uv run pyright src/uv run pytest tests/ -quv build
If you change Python code, aim to run the relevant subset first, then the full suite if the change is broad.
Rules Files
- No
.cursor/rules/directory was found. - No
.cursorrulesfile was found. - No
.github/copilot-instructions.mdfile was found.
If any of those files are added later, treat them as higher-priority repository instructions and update this file.
Coding Style
The codebase follows a straightforward typed Python style with light structure and minimal abstraction.
Imports
- Use
from __future__ import annotationsin Python modules. - Group imports as: standard library, third-party, local package imports.
- Prefer explicit imports over wildcard imports.
- Keep local imports inside functions only when avoiding import cycles or heavy startup cost.
- Use
TYPE_CHECKINGfor type-only imports when helpful, as insrc/optiproxai/logger.py.
Formatting
- Follow Ruff formatting; do not hand-format against the formatter.
- Use 4-space indentation.
- Keep line length formatter-friendly; long calls are wrapped vertically.
- Preserve the existing style of section dividers made from comment banners when editing long modules.
- Prefer concise docstrings on modules, classes, and non-obvious functions.
Types
- Add type hints for public functions, methods, and important locals when clarity helps.
- Use modern Python unions like
str | None, notOptional[str]. - Prefer built-in generics like
list[str],dict[str, Any], andtuple[str, str]. - Use Pydantic
BaseModelfor structured config and API-facing data. - Use dataclasses or enums only where they fit existing patterns; do not introduce new frameworks casually.
- Keep
Anycontained to boundaries like request payloads, YAML data, and flexible JSON structures.
Naming
- Use
snake_casefor functions, methods, variables, and module names. - Use
PascalCasefor classes and Pydantic models. - Use
UPPER_SNAKE_CASEfor module-level constants such as_DEFAULT_TIERand_TIER_ORDER. - Test classes use
Test...naming; test methods usetest_...naming. - Prefer descriptive names over short abbreviations unless the abbreviation is already established in the file.
Control Flow and Design
- Keep functions focused and direct; most modules prefer readable procedural logic over deep indirection.
- Match the current architecture: CLI -> config/router -> scorer/proxy helpers.
- Prefer small private helpers for repeated logic instead of clever abstractions.
- Preserve current public behavior and CLI/API shapes unless the task explicitly changes them.
- Avoid introducing unnecessary dependencies.
Error Handling
- Fail loudly for invalid internal configuration with
ValueErroror assertions where the code already does that. - At HTTP boundaries, return structured OpenAI-style JSON errors rather than raw exceptions.
- Catch narrow exceptions when possible, but match existing patterns at network and file I/O boundaries.
- Log operational failures with the standard
loggingmodule. - For optional integrations, degrade gracefully instead of crashing;
router.pyandlogger.pyalready follow this pattern.
Config and Secrets
- Keep secrets in environment variables via
${VAR}placeholders in YAML; do not hardcode credentials in code. - Preserve config precedence rules: explicit path,
OPTIPROXAI_CONFIG, local config, XDG config, then/etc. - When changing config models, update both validation code and docs/examples if needed.
FastAPI and CLI Conventions
- Keep FastAPI handlers thin; route complex logic into helpers or domain classes.
- Preserve OpenAI-compatible request/response shapes.
- Keep Click commands simple and explicit.
- Prefer JSON-serializable return structures and Pydantic
.model_dump()where already used.
Testing Conventions
- Put tests under
tests/. - Prefer
pyteststyle with plainassertstatements. - Group related tests into
Test...classes. - Use
unittest.mock.MagicMockandpatchfor network-bound or external behavior. - Cover both success paths and graceful fallbacks.
- When adding logic to scoring or routing, add tests for thresholds, edge cases, and fallback behavior.
Agent Advice
- Read the surrounding module before editing; several files use repeated patterns worth preserving.
- Check whether a change affects CLI behavior, config loading, routing behavior, and tests together.
- If you modify API behavior or config semantics, update
README.mdand possiblyCONTRIBUTING.md. - Prefer minimal diffs that fit the current code style.
- Before finishing a meaningful Python change, run lint, format check, typecheck, and the most relevant tests.
Safe Defaults for Agents
- Assume
uvis the canonical way to run all project commands. - Assume
src/optiproxai/is the authoritative source tree. - Assume CI compatibility matters more than local convenience.
- Assume user changes elsewhere in the worktree are intentional; do not revert unrelated edits.