Add input/output token counts to llm.log for AI Gateway responses

What does this MR do and why?

When GitLab's AI features call the AI Gateway, the response includes token usage metadata (input_tokens and output_tokens). This data was never surfaced in llm.log, it was buried in the raw response body string.

This MR extracts those token counts and logs them as dedicated fields in the response_received log event, giving self-hosted customers visibility into token consumption.

Closes #499617

How to set up and validate locally

  1. In the Rails console, enable expanded logging: Feature.enable(:expanded_ai_logging)

  2. Trigger a non-streaming AI call via the Rails console:

    user = User.first 
    client = Gitlab::Llm::AiGateway::Client.new(user, unit_primitive_name: :test, tracking_context: {}) 
    fake_response = { "response" => "Hello", "usage" => { "input_tokens" => 100, "output_tokens" => 42 } }.to_json client.send(:log_response_received, fake_response)
  3. Verify input_tokens and output_tokens appear in the log output: log/llm.log

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading
Loading