API Usage

Google Gemini

URL

https://generativelanguage.googleapis.com/v1beta/models/MODEL_NAME:generateContent?key=API_KEY

API Structure

{
  "contents":[
    {
      "parts":[
        {"text":"User message"},
        {
          "inline_data":{
            "data":"Base64 encoded image string",
            "mime_type":"image/jpeg"
          }
        }
      ],
      "role":"user"
    },
    {
      "parts":[{"text":"Model reply"}],
      "role":"model"
    },
    {
      "parts":[{"text":"User message triggering a function call"}],
      "role":"user"
    },
    {
      "parts":[{
        "functionCall":{
          "args":{
            "argument1":"argument value1",
            "argument2":"argument value2"
          },
          "name":"functionName"
        }
      }],
      "role":"model"
    },
    {
      "parts":[{
        "functionResponse":{
          "name":"functionName",
          "response":{
            "name":"functionName",
            "content": "function return (json object)"
          }
        }
      }],
      "role":"function"
    }
  ],
  "generationConfig":{
    "temperature":0.7,
    "thinkingConfig":{"thinkingBudget":0}
  },
  "safetySettings":[
    {
      "category":"HARM_CATEGORY_SEXUALLY_EXPLICIT",
      "threshold":"OFF"
    },
    {
      "category":"HARM_CATEGORY_HATE_SPEECH",
      "threshold":"OFF"
    },
    {
      "category":"HARM_CATEGORY_HARASSMENT",
      "threshold":"OFF"
    },
    {
      "category":"HARM_CATEGORY_DANGEROUS_CONTENT",
      "threshold":"OFF"
    },
    {
      "category":"HARM_CATEGORY_CIVIC_INTEGRITY",
      "threshold":"OFF"
    }
  ],
  "system_instruction":{"parts":[{"text":"System Prompt"}]},
  "tools":[{
      "function_declarations":[{
        "name":"functionName",
        "description":"function description.",
        "parameters":{
          "type":"object",
          "properties":{
            "argument1":{
              "type":"argument1 type",
              "description":"Argument1 description"
            },
            "argument2":{
              "type":"argument2 type",
              "description":"Argument2 description"
            }
          },
          "required":["argument1"]
        }
      }]
    },
    {
      "googleSearch": {}
    },
    {
      "urlContext": {}
    },
    {
      "codeExecution": {}
    }
  ]
}

If thinkingBudget: -1, model will adjust the budget based on the complexity of the request. The function calling can not be used with other functions (googleSearch, urlContext, codeExecution) simultaneously.

{
  "candidates":[
    {
      "content":{
        "parts":[
          {
            "text":"Response text"
          }
        ],
        "role":"model"
      },
      "finishReason":"STOP",
      "index":0
    }
  ],
  "usageMetadata":{
    "cachedContentTokenCount": 1024,
    "cacheTokensDetails": [{
      "modality": "TEXT",
      "tokenCount": 1024
    }],
    "promptTokenCount":4096,
    "promptTokensDetails":[
      {
        "modality":"TEXT",
        "tokenCount":4096
      }
    ],
    "thoughtsTokenCount": 512,
    "candidatesTokenCount":256,
    "totalTokenCount":4608,
  },
  "modelVersion":"Model name",
  "responseId":"Response id"
}
  • Cached tokens: usageMetadata.cachedContentTokenCount (1024)
  • Non-cached tokens: usageMetadata.promptTokenCount - usageMetadata.cachedContentTokenCount (4096-1024)
  • Thinking tokens: usageMetadata.thoughtsTokenCount (512)
  • Output tokens: usageMetadata.candidatesTokenCount (256)

OpenAI

{
    "Content-Type": "application/json",
    "Authorization": "Bearer API_KEY"
}
{
    "model": "model name",
    "temperature": 1,
    "messages": [
        {"role": "system", "content": "System Prompt"},
        {
            "role": "user", 
            "content": [
                {
                    "text": "User message",
                    "type": "text"
                },
                {
                    "image_url": {"url": "Base64 encoded image string"},
                    "type": "image_url"
                }
            ]
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "function": {
                        "arguments": "function arguments (stringified json object)",
                        "name": "functionName"
                    },
                    "id": "",
                    "type": "function"
                }
            ]
        },
        {
            "content": "function return (stringified json object)",
            "name": "functionName",
            "role": "function"
        },
        {
            "role": "assistant", 
            "content": [{
                "text": "Model reply",
                "type": "text"
            }]
        }
    ],
    "tools": [
        {
            "description": "function description.",
            "name": "functionName",
            "parameters": {
                "properties": {
                    "argument1":{
                        "type":"argument1 type",
                        "description":"Argument1 description"
                    },
                    "argument2":{
                        "type":"argument2 type",
                        "description":"Argument2 description"
                    }
                },
                "required":["argument1"],
                "type": "object"
            }
        }
    ],
    "tool_choice": "auto",
    "reasoning_effort": "none/low/medium/high",
    "extra_body": {
        "google": {
            "thinking_config": {
                "thinking_budget": 0,
                "include_thoughts": true
            }
        }
    }
}
{
    "choices": [
        {
            "message": {
                "content": "Response text",
                "role": "assistant"
            }
        }
    ],
    "model": "gemini-2.5-flash-preview-05-20",
    "usage": {
        "prompt_tokens_details": {
            cached_tokens: 1024
        },
        "completion_tokens": 256,
        "prompt_tokens": 4096,
        "total_tokens": 4864
    }
}
  • Cached tokens: usage.prompt_tokens_details.cached_tokens (1024)
  • Non-cached tokens: usage.prompt_tokens - usage.prompt_tokens_details.cached_tokens (4096-1024)
  • Thinking tokens: usage.total_tokens - usage.prompt_tokens - usage.completion_tokens (512)
  • Output tokens: usage.completion_tokens (256)

Resources