Skip to content

perf(ai): price a request with one load of the settings - #2555

Merged
dan-petty merged 3 commits into
mainfrom
perf/1420-spend-settings-once
Oct 28, 2026
Merged

dan-petty merged 3 commits into
mainfrom
perf/1420-spend-settings-once

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

is_local decides whether a model request ran on local hardware, so the spend path can price it. For a host that its address and its name do not decide, _is_local_host checked the configured cluster domains and AI servers, and _is_configured_cluster_host and _is_configured_ai_server_host each called load_settings. A gateway call with no server also called it in _is_local_gateway_configured, and then twice more for the gateway URL. One priced request thus loaded the settings two or three times, at about 4.4 ms each.

  • is_local makes one _SettingsOnce for each call. It loads the settings on first use (functools.cached_property) and keeps them for the call. The locality helpers take it, and the two configured-host checks take the loaded Settings.
  • A request loads the settings at most once. A loopback or private address, or a name that is not a public DNS name, loads nothing, as before.
  • Each request still loads the settings, so a change to config.yaml takes effect at the next request. No process-wide cache holds the configured hosts.
  • When the settings cannot load, no configured host counts as local, as before. One except Exception in _SettingsOnce replaces three, so the swallowed-failures ceiling of pricing.py falls from 7 to 4.
  • A new test counts load_settings calls with a profile hook, so it patches no devops-cli name.
  • The ledger keeps one connection for each record: a connection costs 0.58 ms of the 14.6 ms that a record takes, and a shared one needs a lock across the threads that record spend. design.md records this and the other rejected alternatives.

Closes #1420

Change: openspec/changes/archive/2026-10-28-1420-spend-settings-once/

`is_local` decides whether a request ran on local hardware. For a host
that its address and its name do not decide, it read the configured
cluster domains and AI servers, and each of two helpers loaded the
settings for that. A gateway call with no server loaded them a third
time. One priced request thus loaded the settings two or three times,
at about 4.4 ms each.

`is_local` now makes one `_SettingsOnce` for each call, which loads
the settings on first use and keeps them for the call. The helpers
take it or the loaded settings. A request loads the settings at most
once, and a local address or a name that is not public loads nothing.
The swallowed-failures ceiling of `pricing.py` falls from 7 to 4.
@dan-petty

Copy link
Copy Markdown
Owner Author

Test evidence:

  • uv run devops ci fail-to-pass --base origin/main: 1 failed on base. test_a_priced_request_loads_the_settings_at_most_once failed with {'hosted': 2, ... 'two hosted servers': 2, ...} != {'hosted': 1, ...}. The other 29 tests in tests/test_ai_spend_pricing.py passed on base.
  • After the rebase onto origin/main d156ce7: the spend, validation, chat-stream and swallowed-failures tests pass (229 passed).

load_settings calls for one request through track_request_spend (a profile hook counts them; the first request of a process also builds the tracer and is not counted):

Request Before After
Hosted provider 2 1
Gateway call that a hosted backend served 2 1
Gateway call with no server 3 1
Two hosted servers 2 1
Local provider 0 0

Five chat requests through LLMClient, with the gateway faked at the HTTP edge: 10 loads before, 5 after. One load_settings call takes about 4.4 ms on the development host.

Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).

@dan-petty
dan-petty force-pushed the perf/1420-spend-settings-once branch from 2dcf704 to 68249d6 Compare October 28, 2026 22:15
@dan-petty
dan-petty marked this pull request as ready for review October 28, 2026 22:23
@dan-petty
dan-petty merged commit 5d86538 into main Oct 28, 2026
17 checks passed
@dan-petty
dan-petty deleted the perf/1420-spend-settings-once branch October 28, 2026 23:18
dan-petty pushed a commit that referenced this pull request Oct 29, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.12.0](v0.11.3...v0.12.0)
(2026-10-29)


### Features

* **ai:** constrain structured replies on the openai and claude
providers ([#2550](#2550))
([4f7390a](4f7390a))
* **argo:** retire the Helm release records of the charts that Argo CD
renders ([#2568](#2568))
([cabfcd4](cabfcd4))
* **k8s:** devops k8s tunnel keeps the cloudflared forward to
k8s.<domain> running
([#2567](#2567))
([f9493fa](f9493fa)),
closes [#1641](#1641)
* **monitoring:** alert when the otel collector fails to export spans,
metric points or log records
([#2565](#2565))
([4fc8ff7](4fc8ff7))
* **telemetry:** the roadmap Service counts its GitHub writes, budget
holds, Firecrawl searches and own deliveries, and caps refine's comments
on one item
([#2556](#2556))
([9819877](9819877)),
closes [#2475](#2475)


### Bug Fixes

* **git:** report a worktree that a run in parallel removed as removed,
not a failed reclaim
([#2557](#2557))
([a556984](a556984)),
closes [#2408](#2408)
* **k8s:** the devops perimeter opens only the collector's OTLP/HTTP
port, 4318
([#2549](#2549))
([d156ce7](d156ce7))
* **sandbox:** devops sandbox logs reads each line with its stream from
a real engine
([#2546](#2546))
([e7216d5](e7216d5)),
closes [#2520](#2520)
* **service:** the webhook route refuses an unsigned or oversized
delivery before it reads the body, and a rate limit guards it
([#2553](#2553))
([40efcd4](40efcd4)),
closes [#2476](#2476)


### Performance Improvements

* **ai:** price a request with one load of the settings
([#2555](#2555))
([5d86538](5d86538)),
closes [#1420](#1420)


### Code Refactoring

* **config:** a default that backs a settings field is read only through
the settings
([#2561](#2561))
([0702d13](0702d13))
* **exceptions:** each exception class declares its error_code and
exit_code ([#2558](#2558))
([410dece](410dece))
* **review:** render the review report from one input, outside the
orchestrator
([#2563](#2563))
([9e73e6d](9e73e6d)),
closes [#1008](#1008)
* **security:** src has no unreviewed Bandit findings, and the gate
scans src at low severity
([#2560](#2560))
([45261dd](45261dd)),
closes [#1538](#1538)
* **security:** the scanners stop looking for mocks, and their tests run
on the scanner fake
([#2551](#2551))
([2b7f37a](2b7f37a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
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.

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

1 participant