refactor(server): move token counting out of algorithms - #316
Conversation
WalkthroughToken counting now uses model-specific ChangesToken counting API and routing cleanup
Estimated code review effort: 4 (Complex) | ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1⚔️ Resolve merge conflicts 💡
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/libsy-llm-client/src/client.rs`:
- Around line 140-167: In count_tokens, validate llm_request.stream before
calling send_encoded and reject true with the existing InvalidRequest error.
Ensure the Anthropic count-token request cannot contain a stream=true field from
preserved or configured body data, while leaving non-streaming requests
unchanged; the later EncodedResponse::Streaming arm can then be removed as
unreachable.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 5fbcec6a-455d-4d74-8f8a-df08ce786d5e
📒 Files selected for processing (12)
crates/libsy-llm-client/src/client.rscrates/libsy/src/algorithms/fall_through.rscrates/libsy/src/algorithms/llm_class.rscrates/libsy/src/algorithms/passthrough.rscrates/libsy/src/algorithms/rand.rscrates/libsy/src/algorithms/stage.rscrates/libsy/src/core/algorithm.rscrates/protocol/src/client.rscrates/switchyard-server/README.mdcrates/switchyard-server/src/config.rscrates/switchyard-server/src/lib.rscrates/switchyard-server/tests/server.rs
💤 Files with no reviewable changes (2)
- crates/protocol/src/client.rs
- crates/libsy/src/core/algorithm.rs
Signed-off-by: nachiketb <nachiketb@nvidia.com>
7acb922 to
ed80f6f
Compare
|
What
Move Anthropic token counting out of libsy algorithms and into the server/client boundary.
count_tokensandcount_tokens_clientfromAlgorithm.RoutedLlmClientandLlmTargetSet.TranslatingLlmClient.Why
Token counting is provider HTTP behavior, not routing-algorithm behavior. The previous design forced every algorithm wrapper to expose a client and let a shared client select an arbitrary model from an internal
HashMap.How
The server considers only a route's completion targets, excluding judge-only targets. It prefers case-insensitive target names or model IDs containing
opus, thensonnet, thenhaiku; ties and unrecognized names preserve route order. The selected model ID is passed directly to the HTTP client. Routes without an Anthropic-format target continue returning400 count_tokens_unsupported.What to review
Validation
cargo test -p switchyard-servercargo clippy --workspace --all-targets -- -D warningscargo fmt --all -- --checkLive differential validation
Compared
mainat702383e8with this MR at7acb9227using the same authenticated upstream and the same structured Anthropic request containing system text, content blocks, and tools.200 {"input_tokens":578}200 {"input_tokens":578}200 {"input_tokens":578}200 {"input_tokens":578}All four server logs were clean. Single-target behavior is semantically and externally identical. For multiple targets, the endpoint contract remains identical while target selection intentionally changes from arbitrary client-map iteration to deterministic Opus preference.
Summary by CodeRabbit
New Features
Bug Fixes
Documentation