Skip to content

Milestone 3 — LiteLLM routing and spend controls

Status: implemented and locally verified (2026-10-22). The explicitly optional live provider compatibility call is PENDING: this checkout has no real provider credential or paid-call authorization. All automated provider tests use deterministic in-process HTTPX fixtures. Work stops before milestone 4; no ACP runner, coding agent, or task workflow replay is implemented.

Delivered

  • Added digest-pinned LiteLLM Proxy v1.83.3-stable with an empty default model list, disabled proxy retries/fallbacks, disabled message-content logging, a credential-free readiness check, and a private provider-egress network. Provider key variables are passed only to LiteLLM; they are absent from the API, worker, web, database, and Redis containers.
  • Added PostgreSQL records and migration b11f10974e82 for provider accounts with environment-variable references, model deployments and declared capabilities/prices, explicit model aliases and ordered fallback deployments, interactive/probe workload pools, project budget policies, project/task/run counters, expiring reservations, and the append-only request ledger.
  • Added typed Chat Completions, Responses, and Anthropic Messages request contracts. Routing checks protocol, streaming, tools, usage reporting, cancellation, and context requirements. No compatible route fails clearly instead of silently lowering requested capabilities.
  • Added bounded per-route attempts, Retry-After handling, deployment cooldowns/circuit breakers, explicit fallback order, interactive capacity reservations, configurable probe overflow, pre-dispatch spend/token reservations, atomic budget accounting, stale-reservation reconciliation, and reservation heartbeats during streamed responses.
  • Added configuration, credential-free status/doctor, project budget, usage, redacted request history, invocation, streaming, and administrator-only explicit live-check APIs. Prompt and completion bodies are not written to the ledger. Provider response details and credentials are not copied into errors or logs.
  • For a stream, fallback can happen only before the first response line. After streaming starts, a failure is surfaced as model.error; no workflow or tool effect is replayed. The worker uses an unconfigured execution adapter, so it does not call the model automatically.
  • Updated the ModelRouter extension contract, architecture, compatibility record, Compose setup, README, and generated TypeScript contracts.

Verification performed

Commands and outcomes are recorded from this checkout. All model-provider behavior was tested without network access to a model provider.

Command Result
uv run ruff check apps/api tests Passed.
uv run python -m compileall -q apps/api/harnessa apps/api/migrations tests Passed.
uv run pytest -q tests/test_model_gateway_contracts.py 3 passed.
TEST_DATABASE_URL=...harnessa_test TEST_REDIS_URL=.../1 uv run pytest -q tests/integration/test_model_gateway.py 8 passed against PostgreSQL 16.10. The provider was HTTPX MockTransport; cases cover 429/503 retries, bounded timeout fallback, request attribution, concurrent project/task/run ceilings, probe capacity and interactive reserve, partial stream failure, and stale reservation settlement.
DATABASE_URL=...harnessa_m3_fresh_test uv run python -m alembic upgrade head Succeeded against an empty PostgreSQL 16.10 database; current revision is b11f10974e82 (head).
DATABASE_URL=...harnessa_m3_upgrade_test uv run python -m alembic upgrade ffac3c1be398, seed an existing org/project/task, then uv run python -m alembic upgrade head Succeeded. Existing task title and description were preserved; migration seeded two workload pools for the existing organization. Final revision is b11f10974e82.
DATABASE_URL=...harnessa_m3_fresh_test uv run python -m alembic check No new upgrade operations detected.
POSTGRES_PORT=25432 REDIS_PORT=26379 API_PORT=28010 WEB_PORT=23000 podman compose --project-name harnessa-m3 --env-file .env.example -f infra/compose.yaml config -q Passed.
HARNESSA_API_URL=http://127.0.0.1:28010 pnpm contracts:generate Passed; generated OpenAPI TypeScript includes the model gateway routes and schemas.
pnpm lint / pnpm typecheck Both passed; ESLint and Ruff clean, web TypeScript clean, mypy clean for 22 API/domain/service/adapter modules.
pnpm test 9 passed, 17 integration tests skipped without service variables, with one existing Starlette/AnyIO BlockingPortal deprecation warning.
TEST_DATABASE_URL=...harnessa_test TEST_REDIS_URL=.../1 pnpm test:integration 17 passed against disposable PostgreSQL 16.10 and Redis 7.4.2 (9 orchestration and 8 model gateway cases).
POSTGRES_PORT=25432 REDIS_PORT=26379 API_PORT=28010 WEB_PORT=23000 podman compose --project-name harnessa-m3 --env-file .env.example -f infra/compose.yaml up --build -d Passed. API, web, worker, database, Redis, and LiteLLM all started. API readiness returned 200 {"status":"ready"}, web returned HTTP 200, and unauthenticated GET /projects returned 401. The web image compiled and prerendered the home, login, and projects routes.
uv run python -m compileall -q apps/api/harnessa apps/api/migrations tests Passed.

Live compatibility check: pending

No paid service was provisioned, no provider key was supplied, and no live model request was sent. To perform one explicitly authorized check after choosing a provider/model:

  1. Put the provider key in the local, untracked .env only (for example OPENAI_API_KEY=...). Never place the value in chat, source files, or the API account request.
  2. Add one local LiteLLM entry to infra/litellm/config.yaml. Set model_name to the deployment's proxy_model, set the provider/model, and use api_key: os.environ/OPENAI_API_KEY (or the matching provider variable). Restart LiteLLM with podman compose -f infra/compose.yaml restart litellm.
  3. As an organization administrator, use POST /ops/models/accounts, POST /ops/models/deployments, POST /ops/models/aliases, and POST /ops/models/pools/{pool_id}/routes to register the matching route. Set a project budget with PUT /projects/{project_id}/models/budget. These endpoints store only the env:VARIABLE_NAME reference and declared price revision.
  4. Sign in through the web UI, obtain the local harnessa_auth and harnessa_csrf cookies, and confirm the alias and project budget. Then run this one-request command with local shell variables set from that session:
export API_ORIGIN='http://localhost:8010'
export WEB_ORIGIN='http://localhost:3000'
export PROJECT_ID='<project UUID>'
export MODEL_ALIAS='<configured alias>'
export AUTH_COOKIE='<local harnessa_auth cookie value>'
export CSRF_TOKEN='<local harnessa_csrf cookie value>'

curl --fail-with-body -sS -X POST \
  "$API_ORIGIN/projects/$PROJECT_ID/models/live-check" \
  -H "Origin: $WEB_ORIGIN" \
  -H "Cookie: harnessa_auth=$AUTH_COOKIE; harnessa_csrf=$CSRF_TOKEN" \
  -H "X-CSRF-Token: $CSRF_TOKEN" \
  -H "Idempotency-Key: live-check-$(date +%s)" \
  -H 'Content-Type: application/json' \
  -d "{\"alias\":\"$MODEL_ALIAS\",\"authorize_paid_request\":true}"

The live-check endpoint requires an administrator membership, a configured project budget, a unique idempotency key, and the explicit authorize_paid_request: true body field. It sends one small Chat Completions request through the same budget reservation and ledger path. The result remains pending until a user with provider credentials and spending authorization runs it.

Limitations and next milestone prerequisites

  • The deployment catalog and LiteLLM config are currently configured separately; proxy_model must match the configured LiteLLM model_name. The doctor verifies gateway readiness and catalog counts, but does not inspect provider credentials or send a model request.
  • Prices and capability declarations are operator-provided metadata. They must match the selected provider deployment. Billing headers and provider usage are recorded when returned; missing usage is settled conservatively as estimated/unknown.
  • A provider request that times out can have unknown provider-side billing. Each such attempt is recorded and reserved spend is conservatively settled. Retry/fallback latency is bounded by configuration but is not promised as a fixed duration.
  • Short-lived scoped runner credentials, ACP execution, cancellation behavior of a concrete agent, tool orchestration, AG-UI projection, Harness² probe integration, and artifact persistence are later work. No tasks or artifacts are created by the model APIs in this milestone.
  • A fresh checkout requires Podman or Docker Compose, network access for pinned images and package installation, local .env configuration, and a disposable _test PostgreSQL database plus Redis database 1 for integration tests. No paid services or real credentials are required for the automated checks.