Skip to content

perf(ai): resolve settings once per LLM request in spend pricing #1420

Description

@dan-petty

Problem

Profiling one devops roadmap refine plan with five model requests showed load_settings called 31 times. track_request_spend calls pricing.is_local, whose _is_local_host re-reads settings about ten times per plan, and the SQLite spend ledger opens and closes a connection per request. That adds an estimated 50-100 ms to every model request, and to every test that drives an AI client, which counts against the 5-minute devops ci budget.

Proposed remedy

Resolve settings once per request and pass the resolved values down the spend path (or read them from the settings object the client already holds). Keep one ledger connection per process or per client, with the locking the ledger already uses.

Acceptance criteria

  • One refine plan with five requests resolves settings at most once per request, measured the way the profile above was (a profile or a count at the settings-file read, not a patched devops-cli function).
  • Spend records and their tests are unchanged.
  • Tests are offline, each call phase stays under 1 s, and uv run devops ci passes.

Found while delivering #1363. Related: #1363.

Proposed design

Problem statement

The spend pricing path calls load_settings multiple times per LLM request. A five-request plan calls it 31 times. The SQLite spend ledger opens and closes a connection per request. This adds 50-100 ms to every model request and to every test that drives an AI client. The extra time counts against the 5-minute devops ci budget.

Acceptance criteria

  • Criterion 1: One refine plan with five requests resolves settings at most once per request.
    Verification: Run a profile or a count at the settings-file read. The count must be at most 5 for five requests. Do not patch a devops-cli function.
  • Criterion 2: Spend records and their tests are unchanged.
    Verification: Run uv run devops ci test tests/test_ai_spend_pricing.py. All tests pass with the same assertions.
  • Criterion 3: Tests are offline and each call phase stays under 1 s.
    Verification: Run uv run devops ci. All tests pass. No test requires network access. Each call phase completes in under 1 s.

Key questions

  • Where in the spend path is load_settings called?: _is_local_host in src/devops_cli/ai/spend/pricing.py:213 reads settings. is_local at line 236 calls it. track_request_spend in src/devops_cli/ai/spend/ledger.py calls pricing.is_local. [sources: src/devops_cli/ai/spend/pricing.py:213, src/devops_cli/ai/spend/pricing.py:236, openspec/changes/archive/2026-10-24-1019-spend-ledger-failed-gateway-attempts/design.md]
  • Should settings be passed down the spend path or read from the client's existing settings object?: Read from the settings object the client already holds. This avoids changing function signatures and keeps the change small. (proposed)
  • Should the ledger connection be per-process or per-client?: Keep one connection per client. The ledger already uses locking, so a per-client connection is safe and simpler than a per-process one. (proposed)
  • How should the measurement work without patching load_settings?: Use a profile or a count at the settings-file read. docs/TESTING.md:107 states that no test patches load_settings or CONFIG_PATH. [sources: docs/TESTING.md:107]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/refactor
    scope/ai
    source/agent
    on Oct 8, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It saves 50-100 ms per request and eases the CI budget, but nothing is broken and no user-facing defect results.
    • Value: Medium. Reduces per-request latency and CI time for every AI client interaction, but the feature already works correctly without it.
    • Effort: Low. The fix is scoped to resolving settings once per request and reusing a single ledger connection, with clear acceptance criteria and no API changes.
  3. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

  4. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at d509dba (see proposed design in issue body).

  5. added
    priority/p2-medium
    status/ready
    effort/medium
    value/high
    on Oct 28, 2026
  6. added a commit that references this issue on Oct 29, 2026
  7. removed
    status/ready
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/aisource/agenttype/refactorvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions