Skip to content

Tag outgoing SDK calls with a lighter-agent-kit User-Agent#1

Open
kutay25 wants to merge 1 commit into
mainfrom
devin/1777918547-tag-user-agent
Open

Tag outgoing SDK calls with a lighter-agent-kit User-Agent#1
kutay25 wants to merge 1 commit into
mainfrom
devin/1777918547-tag-user-agent

Conversation

@kutay25
Copy link
Copy Markdown
Collaborator

@kutay25 kutay25 commented May 4, 2026

Summary

Brand every HTTP request the kit issues through lighter-python with a User-Agent header so the server can attribute traffic to the agent kit (vs. web/mobile/arbitrary SDK callers).

Header format (RFC 7231 product-tokens):

User-Agent: lighter-agent-kit/0.1.0 lighter-python/1.0.8

Filter agent-kit traffic server-side with ^lighter-agent-kit/; the trailing token also lets you slice by SDK version.

Implementation:

  • New helper tag_api_client(api_client) in scripts/_sdk.py that sets api_client.user_agent (the existing public setter on lighter.ApiClient).
  • Applied at every lighter.ApiClient(...) and lighter.SignerClient(...) construction site (5 total: query.py ×2, paper.py ×2, trade.py ×1). For SignerClient the helper is applied to signer.api_client since the SDK constructs the inner ApiClient itself.
  • SDK version resolved via importlib.metadata.version("lighter-sdk") so it tracks the actually-installed package; falls back to lighter.__version__ then "unknown".
  • No changes to lighter-python. Uses the existing OpenAPI-generated user_agent property setter, which is the only existing source-tag mechanism in the SDK today.
  • Two test files needed a new entry on the in-memory _sdk stub (sdk_mod.tag_api_client = lambda c: c) since they reconstruct _sdk with types.ModuleType.

End-to-end verification. Spun up a local HTTP capture server, pointed LIGHTER_HOST at it, and exercised every public-read entrypoint. All outgoing requests carried the new UA:

$ LIGHTER_HOST=http://127.0.0.1:29292 python3 scripts/query.py system status
[capture] GET /                  UA='lighter-agent-kit/0.1.0 lighter-python/1.0.8'

$ LIGHTER_HOST=http://127.0.0.1:29292 python3 scripts/query.py market list
[capture] GET /api/v1/orderBooks UA='lighter-agent-kit/0.1.0 lighter-python/1.0.8'

$ LIGHTER_HOST=http://127.0.0.1:29292 python3 scripts/query.py market stats
[capture] GET /api/v1/exchangeStats UA='lighter-agent-kit/0.1.0 lighter-python/1.0.8'

$ LIGHTER_HOST=http://127.0.0.1:29292 python3 scripts/paper.py refresh BTC
[capture] GET /api/v1/orderBooks UA='lighter-agent-kit/0.1.0 lighter-python/1.0.8'
[capture] GET /api/v1/orderBooks UA='lighter-agent-kit/0.1.0 lighter-python/1.0.8'

Default boilerplate UA before this change was OpenAPI-Generator/1.0.0/python (set by the auto-generated lighter/api_client.py:91).

pytest tests/ → 73 passed.

Review & Testing Checklist for Human

  • Confirm the chosen UA format (lighter-agent-kit/<version> lighter-python/<sdk>) is what you want logged/aggregated server-side; locking it in here means existing dashboards/regexes will need to match ^lighter-agent-kit/.
  • Sanity-check AGENT_KIT_VERSION = "0.1.0" placement in scripts/_sdk.py — this is now the single source of truth for the kit's version (replaces the literal in scripts/health.py, though this PR leaves health.py alone to keep the diff minimal). If you want to consolidate, follow up with a tiny PR.
  • Run python3 scripts/query.py market list (or any read) against staging or mainnet and confirm a real request appears in your traffic logs with the expected UA.
  • Decide whether trade.py writes (signed orders) should also be sampled to confirm the UA propagates on POST /sendTx* paths — this PR applies the same tag to SignerClient.api_client, so they should, but a single signed test order against testnet would close the loop end-to-end.

Notes

  • WebSocket calls (lighter/ws_client.py) don't carry any tag and aren't used by agent-kit; out of scope.
  • The helper is idempotent and returns the client, so it's safe to layer on top later (e.g. per-script granularity like lighter-agent-kit-trade/...) if you ever want that.
  • Picked the User-Agent header rather than introducing a new X-Source header because UA is the only existing tagging convention in the SDK and presumably what web/mobile already use.

Link to Devin session: https://app.devin.ai/sessions/6048677bfdf448c88ba064cc4e9ea0bf
Requested by: @kutay25

Brand every HTTP request the kit issues through lighter-python with a
`User-Agent: lighter-agent-kit/<version> lighter-python/<sdk>` header
so the server can attribute traffic to the agent kit (vs. web/mobile/
arbitrary SDK callers).

Implementation lives in scripts/_sdk.py as a single helper applied at
each ApiClient/SignerClient construction site. SDK version is read via
importlib.metadata so it tracks the actually-installed package, with a
fallback to lighter.__version__ and finally 'unknown'.
@devin-ai-integration
Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant