docling icon indicating copy to clipboard operation
docling copied to clipboard

Get the total tokens used in the image description

Open ezequiel88 opened this issue 4 months ago • 3 comments

Currently, I only get the result of the image description in the pipeline.

I also need to get the total tokens consumed in the request to the remote model.

Example:

return {
        "response": res.choices[0].message.content,
        "tokens": {
            "prompt": res.usage.prompt_tokens,
            "completion": res.usage.completion_tokens,
            "total": res.usage.total_tokens,
        }
    }

ezequiel88 avatar Sep 16 '25 14:09 ezequiel88

The image description pipeline currently does not return token usage (prompt, completion, total tokens) in its response. While the remote model API may provide this information in a usage field (as in OpenAI-compatible APIs), the pipeline only returns the generated text and does not propagate token usage data through its models or responses. To include token usage in your output, you would need to modify the pipeline to extract the usage field from the remote API response and add it to your result object, updating the relevant data models and response logic to support this structure. For remote APIs that do not provide token usage, you may need to estimate or handle missing values as appropriate. For more details on the expected response structure and proxying non-OpenAI APIs, see this discussion and this issue.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Sep 16 '25 14:09 dosubot[bot]

Hi @ezequiel88,

You can capture token usage from the API using a custom plugin approach. I've published a working implementation here:

https://github.com/FrigaZzz/custom-docling-plugins

The plugin extends Docling's image describer to intercept the API response and extract the usage field containing prompt_tokens, completion_tokens, and total_tokens before the pipeline processes it.

The repository includes:

  • Custom plugin implementation that captures token metrics
  • Setup instructions for integrating with Docling's pipeline
  • Examples showing how to access the telemetry data

This lets you track token consumption without modifying Docling's core code. Check the repo for the complete implementation details.

FrigaZzz avatar Oct 07 '25 15:10 FrigaZzz

@FrigaZzz veja isso por gentileza.

Grato pelo Retorno

https://github.com/FrigaZzz/custom-docling-plugins/issues/1

ezequiel88 avatar Dec 01 '25 15:12 ezequiel88