Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
fix(stats): record noop routes in /v1/routing/stats (#109)
Elyas Mehtabuddin8baf8ec274e7
diff --git a/switchyard/lib/profiles/noop.py b/switchyard/lib/profiles/noop.pyindex 70ffc142f..16580589a 100644--- a/switchyard/lib/profiles/noop.py+++ b/switchyard/lib/profiles/noop.py@@ -35,7 +35,7 @@ _NOOP_CONTENT = "pong"_NOOP_MODEL = "noop"-def _make_completion(*, request_id: str, created: int) -> ChatCompletion:+def _make_completion(request_id: str, created: int) -> ChatCompletion:"""Build the fixed no-op non-streaming completion."""return ChatCompletion(id=f"chatcmpl-{request_id}",@@ -54,7 +54,6 @@ def _make_completion(*, request_id: str, created: int) -> ChatCompletion:async def _noop_stream(- *,request_id: str,created: int,) -> AsyncIterator[ChatCompletionChunk]:@@ -115,14 +114,15 @@ class NoopProfile:def __init__(self,- *,request_processors: tuple[Any, ...] = (),response_processors: tuple[Any, ...] = (),+ stats_accumulator: StatsAccumulator | None = None,) -> None:"""Create a no-op profile with a local translation helper."""self._translation = TranslationEngine()self._request_processors = request_processorsself._response_processors = response_processors+ self._stats_accumulator = stats_accumulatordef iter_components(self) -> list[object]:"""Return lifecycle components in startup order."""@@ -162,6 +162,7 @@ class NoopProfile:return NoopProfile(request_processors=tuple(request_chain),response_processors=tuple(response_chain),+ stats_accumulator=stats if enable_stats else None,)async def process(self, input: ProfileInput) -> NoopProcessedRequest:@@ -217,6 +218,9 @@ class NoopProfile:) -> ChatResponse:"""Execute no-op response generation with an existing context."""processed = await self.process_with_context(input, ctx)+ # Stamp ctx.selected_model so the stats response processor attributes+ # the response tokens to "noop" instead of "<unknown>".+ ctx.selected_model = _NOOP_MODELopenai_request = self._translation.request_to_any_of(processed.request,_NOOP_SUPPORTED_TYPES,@@ -235,6 +239,12 @@ class NoopProfile:response = ChatResponse.openai_completion(_make_completion(request_id=request_id, created=created))+ # Count the request only after the response is built, so a translation+ # failure does not leave a spurious success in /v1/routing/stats. Record+ # success before rprocess records tokens, which is the order the stats+ # response processor's record_usage_after_success_attribution expects.+ if self._stats_accumulator is not None:+ await self._stats_accumulator.record_success(_NOOP_MODEL)return await self.rprocess(processed, response)diff --git a/tests/test_noop_llm_backend.py b/tests/test_noop_llm_backend.pyindex 695218129..0a5c4a362 100644--- a/tests/test_noop_llm_backend.py+++ b/tests/test_noop_llm_backend.py@@ -4,11 +4,12 @@"""Unit tests for the no-op profile."""from switchyard.lib.profiles import NoopProfile, NoopProfileConfig, ProfileSwitchyard+from switchyard.lib.stats_accumulator import StatsAccumulatorfrom switchyard_rust.core import ChatRequest, ChatResponseType, response_type_matchesfrom switchyard_rust.profiles import ProfileInput-def _openai_request(*, stream: bool = False) -> ChatRequest:+def _openai_request(stream: bool = False) -> ChatRequest:return ChatRequest.openai_chat({"model": "noop", "messages": [{"role": "user", "content": "ping"}], "stream": stream})@@ -106,3 +107,22 @@ class TestNoOpProfile:assert processed.request.model == "noop"assert processed_response is response++ async def test_noop_call_reports_routing_stats(self) -> None:+ """The no-op profile self-reports its call and model to the stats accumulator."""+ stats = StatsAccumulator()+ profile = NoopProfileConfig().build().with_runtime_components(+ stats_accumulator=stats+ )++ await ProfileSwitchyard(profile).call(_openai_request(stream=False))++ snap = stats.snapshot_sync()+ assert snap["total_requests"] == 1+ assert "noop" in snap["models"]+ assert snap["models"]["noop"]["calls"] == 1+ assert "<unknown>" not in snap["models"]+ # Response tokens land on "noop", not "<unknown>".+ assert snap["models"]["noop"]["prompt_tokens"] == 1+ assert snap["models"]["noop"]["completion_tokens"] == 1+ assert snap["models"]["noop"]["total_tokens"] == 2