Skip to content

AiMessages do not have token information #22

@dsantiago

Description

@dsantiago

If I use the raw invocation I can get the tokens but with an AiMessage response from the langchain-oracle framework I don't see where I retrieve the total token... Can we add it to AiMessage response?

Wit this script:

generative_ai_inference_client = oci.generative_ai_inference.GenerativeAiInferenceClient(
    config={}, 
    signer=signer, 
    service_endpoint=endpoint, 
    retry_strategy=oci.retry.NoneRetryStrategy()
)
chat_detail = oci.generative_ai_inference.models.ChatDetails()

content = oci.generative_ai_inference.models.TextContent()
content.text = "What is the capital of France?"
message = oci.generative_ai_inference.models.Message()
message.role = "USER"
message.content = [content]

chat_request = oci.generative_ai_inference.models.GenericChatRequest()
chat_request.api_format = oci.generative_ai_inference.models.BaseChatRequest.API_FORMAT_GENERIC
chat_request.messages = [message]
chat_request.max_tokens = 4000
chat_request.temperature = 0
chat_request.frequency_penalty = 0
chat_request.presence_penalty = 0
chat_request.top_p = 1
chat_request.top_k = 0

chat_detail.serving_mode = oci.generative_ai_inference.models.OnDemandServingMode(model_id=models['xai.grok-3'])
chat_detail.chat_request = chat_request
chat_detail.compartment_id = compartment_id

chat_response = generative_ai_inference_client.chat(chat_detail)
print(chat_response.data)

I get this output:

{
  "chat_response": {
    "api_format": "GENERIC",
    "choices": [
      {
        "finish_reason": "stop",
        "index": 0,
        "logprobs": {
          "text_offset": null,
          "token_logprobs": null,
          "tokens": null,
          "top_logprobs": null
        },
        "message": {
          "content": [
            {
              "text": "The capital of France is Paris.",
              "type": "TEXT"
            }
          ],
          "name": null,
          "refusal": null,
          "role": "ASSISTANT",
          "tool_calls": null
        },
        "usage": null
      }
    ],
    "time_created": "2025-08-24T17:23:09.711000+00:00",
    "usage": {
      "completion_tokens": 8,
      "completion_tokens_details": null,
      "prompt_tokens": 18,
      "prompt_tokens_details": null,
      "total_tokens": 26
    }
  },
  "model_id": "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyayjawvuonfkw2ua4bob4rlnnlhs522pafbglivtwlfzta",
  "model_version": "1.0.0"
}

There we can see the total_tokens entry.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions