chat_completion_headers.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Non-streaming chat completions that still feed response headers to the credits capture."""
  2. from __future__ import annotations
  3. import logging
  4. logger = logging.getLogger(__name__)
  5. def _chat_completion_capturing_headers(agent, client, api_kwargs: dict):
  6. """Non-streaming ``chat.completions.create`` that also feeds the response headers to the
  7. rate-limit / credits capture, as ``_chat_stream_created`` does for streams.
  8. The parsed ``ChatCompletion`` drops headers, so without this a non-streaming turn never
  9. observes ``x-nous-credits-*`` (usage-window ``spend_source`` / gauges go stale). The SDK's
  10. ``with_raw_response`` returns the same request with headers attached; a client without it keeps
  11. the plain call. Usage-window plans
  12. only: every other provider, and a Nous session not yet known to be on a window plan (the
  13. session-start seed or a stream header establishes it), keep the plain call; headers are
  14. captured only when the response itself is the usage-window kind. A capture failure never
  15. fails the turn.
  16. """
  17. # `is True`: a Mock agent's invented attribute is not a window plan.
  18. state = getattr(agent, "_credits_state", None)
  19. if getattr(agent, "provider", "") != "nous" or getattr(state, "is_usage_window", False) is not True:
  20. return client.chat.completions.create(**api_kwargs)
  21. raw_api = getattr(getattr(getattr(client, "chat", None), "completions", None), "with_raw_response", None)
  22. if raw_api is None:
  23. return client.chat.completions.create(**api_kwargs)
  24. raw = raw_api.create(**api_kwargs)
  25. from agent.credits_tracker import response_is_usage_window
  26. if not response_is_usage_window(raw):
  27. return raw.parse()
  28. for capture in (agent._capture_rate_limits, agent._capture_credits,
  29. agent._capture_nous_model_switch, agent._check_openrouter_cache_status):
  30. try:
  31. capture(raw)
  32. except Exception:
  33. logger.debug("non-streaming header capture failed", exc_info=True)
  34. return raw.parse()