feat(usage): add feature attribution header to API requests - #407
Conversation
Code Review SummaryStatus: No New Issues Found | Recommendation: Merge (after addressing existing comments) OverviewThis PR adds feature attribution tracking via a
The two existing inline comments (about Files Reviewed (7 files)
|
| export const HEADER_FEATURE = "X-KILOCODE-FEATURE" | ||
|
|
||
| /** Default feature value (CLI usage) */ | ||
| export const DEFAULT_FEATURE = "cli" |
There was a problem hiding this comment.
I am wondering if we should instead of a default use 'unknown' so that when new products roll out and we forget this change, we see it in the data
There was a problem hiding this comment.
agreed, and I thought I had changed that. thanks for catching!
There was a problem hiding this comment.
The way I've found to derive cli feature while keeping misattributed new products as unkjonwn is to check if env var is not set and there's no serve command. does that make sense to you?
Introduce X-KILOCODE-FEATURE header for tracking request origin across different entry points (CLI, VSCode extension, autocomplete). - Add HEADER_FEATURE, DEFAULT_FEATURE, and ENV_FEATURE constants - Add getFeatureHeader() helper reading from KILOCODE_FEATURE env var - Include feature header in buildKiloHeaders() when env var is set - Set feature to "autocomplete" for FIM/completion requests - Set KILOCODE_FEATURE="vscode-extension" when spawning CLI from VSCode - Default to "cli" feature in opencode entry point - Add unit tests for feature header logic
…rom command context
Remove the hardcoded DEFAULT_FEATURE ("cli") constant from kilo-gateway
and instead derive the feature value at runtime in opencode. When the
KILOCODE_FEATURE env var is not set, the "serve" command is now tagged
as "unknown" (to surface misconfiguration when a caller forgets to set
the env var), while all other commands default to "cli".
88d9964 to
1be28bb
Compare
Summary
Adds the
X-KILOCODE-FEATUREheader to all LLM requests going through kilo-gateway, enabling per-feature attribution of token usage inmicrodollar_usage. This is Step 7 of the Microdollar Feature Tracking plan.Problem
There's no way to distinguish which feature generates each token usage record in
microdollar_usage. All requests from the CLI, VS Code extension, and cloud features look identical at the gateway level.Solution
Every LLM request now includes an
X-KILOCODE-FEATUREheader identifying the calling feature. The value is determined by theKILOCODE_FEATUREenvironment variable, which callers set before spawning the kilo CLI process.Changes
packages/kilo-gateway/src/api/constants.tsHEADER_FEATURE,DEFAULT_FEATURE,ENV_FEATUREconstantspackages/kilo-gateway/src/headers.tsgetFeatureHeader()— readsprocess.env.KILOCODE_FEATURE, returnsundefinedwhen not setbuildKiloHeaders()conditionally includes the feature header only when the env var is setgetFeatureHeader()to prevent misattribution — missing env var = no header = NULL in DBpackages/kilo-gateway/src/server/routes.ts/kilo/fim) now includesbuildKiloHeaders()+ hardcoded[HEADER_FEATURE]: "autocomplete"overridefetch()calls without any kilo headerspackages/kilo-vscode/src/services/cli-backend/server-manager.tsKILOCODE_FEATURE: "vscode-extension"to the spawn env when launching the kilo CLI processpackages/opencode/src/index.tsprocess.env.KILOCODE_FEATURE = "cli"if not already set by a callerpackages/kilo-gateway/src/index.tsgetFeatureHeaderpackages/kilo-gateway/src/headers.test.ts(new)getFeatureHeader()andbuildKiloHeaders()feature header behaviorTesting
bun test src/headers.test.ts)