Title: TokenUsage
Published: May 20, 2026

---

# class TokenUsage {}

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#description)
 * [Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#wp--skip-link--target)

Represents token usage statistics for an AI operation.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#description)󠁿

This DTO tracks the number of tokens used in prompts and completions, which is important
for monitoring usage and costs.

Note that thought tokens are a subset of completion tokens, not additive.
In other
words: completionTokens – thoughtTokens = tokens of actual output content.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#methods)󠁿

| Name | Description | 
| [TokenUsage::__construct](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/__construct/) | Constructor. | 
| [TokenUsage::fromArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/fromarray/) | {@inheritDoc} | 
| [TokenUsage::getCompletionTokens](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/getcompletiontokens/) | Gets the number of completion tokens, including any thought tokens. | 
| [TokenUsage::getJsonSchema](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/getjsonschema/) | {@inheritDoc} | 
| [TokenUsage::getPromptTokens](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/getprompttokens/) | Gets the number of prompt tokens. | 
| [TokenUsage::getThoughtTokens](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/getthoughttokens/) | Gets the number of thought tokens, which is a subset of the completion token count. | 
| [TokenUsage::getTotalTokens](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/gettotaltokens/) | Gets the total number of tokens. | 
| [TokenUsage::toArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/toarray/) | {@inheritDoc} |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#source)󠁿

    ```php
    class TokenUsage extends AbstractDataTransferObject
    {
        public const KEY_PROMPT_TOKENS = 'promptTokens';
        public const KEY_COMPLETION_TOKENS = 'completionTokens';
        public const KEY_TOTAL_TOKENS = 'totalTokens';
        public const KEY_THOUGHT_TOKENS = 'thoughtTokens';
        /**
         * @var int Number of tokens in the prompt.
         */
        private int $promptTokens;
        /**
         * @var int Number of tokens in the completion, including any thought tokens.
         */
        private int $completionTokens;
        /**
         * @var int Total number of tokens used.
         */
        private int $totalTokens;
        /**
         * @var int|null Number of tokens used for thinking, as a subset of completion tokens.
         */
        private ?int $thoughtTokens;
        /**
         * Constructor.
         *
         * @since 0.1.0
         *
         * @param int $promptTokens Number of tokens in the prompt.
         * @param int $completionTokens Number of tokens in the completion, including any thought tokens.
         * @param int $totalTokens Total number of tokens used.
         * @param int|null $thoughtTokens Number of tokens used for thinking, as a subset of completion tokens.
         */
        public function __construct(int $promptTokens, int $completionTokens, int $totalTokens, ?int $thoughtTokens = null)
        {
            $this->promptTokens = $promptTokens;
            $this->completionTokens = $completionTokens;
            $this->totalTokens = $totalTokens;
            $this->thoughtTokens = $thoughtTokens;
        }
        /**
         * Gets the number of prompt tokens.
         *
         * @since 0.1.0
         *
         * @return int The prompt token count.
         */
        public function getPromptTokens(): int
        {
            return $this->promptTokens;
        }
        /**
         * Gets the number of completion tokens, including any thought tokens.
         *
         * @since 0.1.0
         *
         * @return int The completion token count.
         */
        public function getCompletionTokens(): int
        {
            return $this->completionTokens;
        }
        /**
         * Gets the total number of tokens.
         *
         * @since 0.1.0
         *
         * @return int The total token count.
         */
        public function getTotalTokens(): int
        {
            return $this->totalTokens;
        }
        /**
         * Gets the number of thought tokens, which is a subset of the completion token count.
         *
         * @since 1.3.0
         *
         * @return int|null The thought token count or null if not available.
         */
        public function getThoughtTokens(): ?int
        {
            return $this->thoughtTokens;
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public static function getJsonSchema(): array
        {
            return ['type' => 'object', 'properties' => [self::KEY_PROMPT_TOKENS => ['type' => 'integer', 'description' => 'Number of tokens in the prompt.'], self::KEY_COMPLETION_TOKENS => ['type' => 'integer', 'description' => 'Number of tokens in the completion, including any thought tokens.'], self::KEY_TOTAL_TOKENS => ['type' => 'integer', 'description' => 'Total number of tokens used.'], self::KEY_THOUGHT_TOKENS => ['type' => 'integer', 'description' => 'Number of tokens used for thinking, as a subset of completion tokens.']], 'required' => [self::KEY_PROMPT_TOKENS, self::KEY_COMPLETION_TOKENS, self::KEY_TOTAL_TOKENS]];
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         *
         * @return TokenUsageArrayShape
         */
        public function toArray(): array
        {
            $data = [self::KEY_PROMPT_TOKENS => $this->promptTokens, self::KEY_COMPLETION_TOKENS => $this->completionTokens, self::KEY_TOTAL_TOKENS => $this->totalTokens];
            if ($this->thoughtTokens !== null) {
                $data[self::KEY_THOUGHT_TOKENS] = $this->thoughtTokens;
            }
            return $data;
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public static function fromArray(array $array): self
        {
            static::validateFromArrayData($array, [self::KEY_PROMPT_TOKENS, self::KEY_COMPLETION_TOKENS, self::KEY_TOTAL_TOKENS]);
            return new self($array[self::KEY_PROMPT_TOKENS], $array[self::KEY_COMPLETION_TOKENS], $array[self::KEY_TOTAL_TOKENS], $array[self::KEY_THOUGHT_TOKENS] ?? null);
        }
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/results/dto/tokenusage.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/php-ai-client/src/Results/DTO/TokenUsage.php#L27)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Results/DTO/TokenUsage.php#L27-L144)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-tokenusage/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.1.0](https://developer.wordpress.org/reference/since/0.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwordpress-aiclient-results-dto-tokenusage%2F)
before being able to contribute a note or feedback.