OpenAI compatibility for labels

Hi there, I what to add custom metadata for cost accounting via OpenAI compatibility API. I would like to know if the metadata field defined in the OpenAI compatibility API has the same effect as the labels field as defined generateContent API .

1 Like

Hey @lvqier ,

Welcome to Forum!

Great question! The metadata field in the OpenAI-compatible API doesn’t behave exactly like the labels field in generateContent . If you’re using it for cost tracking or analytics, it’s best to confirm with the Gemini API docs or support to ensure it’s properly logged and interpreted.

Thanks!

1 Like

Hi @Deepakishore ,
Thanks for reply!
I can confirm that the Gemini API docs do not mention anything about this. Are there any approach to find out how to do cost tracking in the OpenAI-compatible API ?

1 Like

Hey @lvqier — thanks for confirming! Since the docs don’t cover this clearly, one workaround is to log your requests manually with custom metadata on your backend. You can tag each request with a user ID or project label and track token usage separately. It’s not built-in, but it gives you control over cost tracking until official support improves.

1 Like

Hello,

Welcome to the Forum!

Could you please share your code where you are passing the metadata argument in OpenAI compatibility?

1 Like

@Lalit_Kumar Sure, I have tried the following two methods, but neither works:

  1. metadata defined by OpenAI
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
  https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/openapi/chat/completions \
  -d '{
    "model": "google/${MODEL_ID}",
    "messages": [{
      "role": "user",
      "content": "Write a story about a magic backpack."
    }],
    "metadata": {
      "team": "91871"
    }
  }'
  
  1. labels defined by generateContent API:
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
  https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/openapi/chat/completions \
  -d '{
    "model": "google/${MODEL_ID}",
    "messages": [{
      "role": "user",
      "content": "Write a story about a magic backpack."
    }],
    "extra_body": {
      "google": {
        "labels": {
          "team": "91871"
        }
      }
    }
  }'
  
1 Like

Hello,

Currently “label” parameter is not supported with OpenAI compatibility, but I would recommend you to keep checking Gemini API documentation as we are constantly improving and adding new features.

1 Like