usage.go 772 B

12345678910111213141516171819202122232425
  1. package xai
  2. // IncludeIndependentReasoningTokens adds reasoning tokens to billed output
  3. // only when total_tokens proves they are not already folded into output.
  4. //
  5. // Official xAI Chat Completions example: prompt=32, completion=9,
  6. // reasoning=94, total=135. Responses example: input=32, output=9,
  7. // reasoning=110, total=151. OpenAI's canonical completion_tokens already
  8. // includes reasoning and total equals input+output.
  9. func IncludeIndependentReasoningTokens(input, output, total, reasoning int64) int64 {
  10. if input < 0 || output < 0 || reasoning <= 0 || total <= 0 {
  11. return output
  12. }
  13. if total == input+output {
  14. return output
  15. }
  16. gap := total - input - output
  17. if gap <= 0 {
  18. return output
  19. }
  20. if reasoning < gap {
  21. gap = reasoning
  22. }
  23. return output + gap
  24. }