OpenAI is doubling my token usage

I thought for sure this was a problem in my code logic, and it still might be, but I can say for sure that OpenAI logs reflect double the token usage that my logs reflect.

I put a logger on my method that does ALL the OpenAI API calls. At 7:00pm PST it only recorded two API calls to GPT-4. Here are the model responses for each call:

My code corroborates this by tracking the token count per log id processed:

And, the total in my master log reflects the total tokens calculated for that model at 7:00pm:

You can see the token amounts match up perfectly, but yet in the OpenAI usage dashboard, I see this:

openai log

As you can see, the number of API calls and the total tokens are EXACTLY double. Each and every time.

So, this is the code that calls the curl command that actually makes the call:

		// Your existing code to execute the CURL request
		$responseBody = $this->solrai_executeCurlRequest($url, $headers, $data);

		# Debug
		# Log the response body
		$logger->notice('(generateResponseOpenAI) Response: @response', ['@response' => print_r($responseBody, TRUE)]);

I include this to verify that what I see in the logs reflects the actual curl API calls that I am making.

This is the curl method:

	 * Executes a cURL request.
	 * @param string $url
	 *   The URL to make the request to.
	 * @param array $headers
	 *   An array of HTTP headers.
	 * @param array $data
	 *   The data to send in the request body.
	 * @return array
	 *   The response from the server, decoded from JSON to an array.
	public function solrai_executeCurlRequest($url, $headers, $data) {
	  $curl = curl_init();

	  curl_setopt($curl, CURLOPT_URL, $url);
	  curl_setopt($curl, CURLOPT_POST, true);
	  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
	  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	  curl_setopt($curl, CURLOPT_ENCODING, '');
	  curl_setopt($curl, CURLOPT_MAXREDIRS, 10);

	  $startTime = microtime(true);

	  $response = curl_exec($curl);

	  $endTime = microtime(true);
	  $elapsedTime = $endTime - $startTime;
	  // Add to global variable
	  $this->elapsedTime = $elapsedTime;

	  $error_msg = null;
	  try {
		if (curl_errno($curl)) {
		  $error_msg = curl_error($curl);
		  throw new \Exception('cURL error: ' . $error_msg);
	  } catch (\Exception $e) {


		if ($this->logElapsedTime === true) {
			// Extract the base domain from the URL
			$host = parse_url($url, PHP_URL_HOST);
			$baseDomain = implode('.', array_slice(explode('.', $host), -2));
			// Extract the model from the data array
			$model = isset($data['model']) ? $data['model'] : 'Not provided';
			// Log the information
			$this->loggerFactory->get('solrai')->info('API: ' . $baseDomain . ' | Elapsed time: ' . $elapsedTime . ' seconds' . ' | Model: ' . $model . ' | Method: ' . __FUNCTION__ . ' | Endpoint: ' . $url);
	  if ($error_msg) {
		return ['error' => $error_msg];

	  $responseBody = json_decode($response, true);

	  // Append elapsed time to the response body
	  $responseBody['_elapsed_time'][$url] = $elapsedTime;

	  return $responseBody;

The only way I can see that this would be a problem on my end is if this method is executing twice, but only returning the response once. I’ve never seen that before, but I imagine anything is possible. Or, there is something about this request that makes OpenAI duplicate the response in it’s logs, even though it only sends back one response.

At any rate, I need some help figuring this out.

Figured it out. Whew! The problem was, in fact, on my end buried deep, deep down in the code. I’m just glad it didn’t turn out that my curl function was failing!

1 Like