LLM Usage: cached tokens #21520

Open
opened 2026-02-21 20:12:57 -05:00 by yindo · 1 comment
Owner

Originally created by @dvisigalli on GitHub (Jan 8, 2026).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • Please do not modify this template :) and fill in all the required fields.

1. Is this request related to a challenge you're experiencing? Tell me about your story.

Reading the docs at https://docs.dify.ai/en/develop-plugin/features-and-specs/plugin-types/model-schema
i found out that LLMUsage class has the following fields

class LLMUsage(ModelUsage):
"""
Model class for llm usage.
"""
prompt_tokens: int # Tokens used by prompt
prompt_unit_price: Decimal # Prompt unit price
prompt_price_unit: Decimal # Prompt price unit, i.e., unit price based on how many tokens
prompt_price: Decimal # Prompt cost
completion_tokens: int # Tokens used by completion
completion_unit_price: Decimal # Completion unit price
completion_price_unit: Decimal # Completion price unit, i.e., unit price based on how many tokens
completion_price: Decimal # Completion cost
total_tokens: int # Total tokens used
total_price: Decimal # Total cost
currency: str # Currency unit
latency: float # Request time (s)

and the fields match the keys that i find in a LLM "node_finish" json.

I wonder why there's no provision for cached token keys (e.g., cached_tokens, cached_tokens_price_unit).
How is the ability to leverage prompt caching handled?
Is it possible to have these specific entries available in the LLMUsage class mapping?

2. Additional context or comments

No response

3. Can you help us with this feature?

  • I am interested in contributing to this feature.
Originally created by @dvisigalli on GitHub (Jan 8, 2026). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] Please do not modify this template :) and fill in all the required fields. ### 1. Is this request related to a challenge you're experiencing? Tell me about your story. Reading the docs at https://docs.dify.ai/en/develop-plugin/features-and-specs/plugin-types/model-schema i found out that LLMUsage class has the following fields class LLMUsage(ModelUsage): """ Model class for llm usage. """ prompt_tokens: int # Tokens used by prompt prompt_unit_price: Decimal # Prompt unit price prompt_price_unit: Decimal # Prompt price unit, i.e., unit price based on how many tokens prompt_price: Decimal # Prompt cost completion_tokens: int # Tokens used by completion completion_unit_price: Decimal # Completion unit price completion_price_unit: Decimal # Completion price unit, i.e., unit price based on how many tokens completion_price: Decimal # Completion cost total_tokens: int # Total tokens used total_price: Decimal # Total cost currency: str # Currency unit latency: float # Request time (s) and the fields match the keys that i find in a LLM "node_finish" json. I wonder why there's no provision for cached token keys (e.g., cached_tokens, cached_tokens_price_unit). How is the ability to leverage prompt caching handled? Is it possible to have these specific entries available in the LLMUsage class mapping? ### 2. Additional context or comments _No response_ ### 3. Can you help us with this feature? - [ ] I am interested in contributing to this feature.
yindo added the 📚 documentationstale labels 2026-02-21 20:12:57 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 8, 2026):

Hi @dvisigalli! I'm Dosu and I’m helping the dify team.

Currently, the LLMUsage class does not include fields for cached tokens or any caching-related metrics, and there is no built-in prompt caching or cached-token tracking in Dify's codebase or plugin API. All tracked usage fields (prompt/completion tokens, prices, etc.) are modeled directly from the LLM response and do not account for cache hits or cache-based pricing. If you want to track cached tokens or cache-related costs, you would need to extend the LLMUsage model and its metadata, and update the relevant logic to populate and consume these new fields in your own fork or via a plugin layer. There is no official extension point or pattern for this yet, so any such integration would be a custom modification at this time.
Reference: Dify docs and code
Reference: LLMUsage class in code

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 8, 2026): <!-- Greeting --> Hi @dvisigalli! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify team. <!-- Answer --> Currently, the LLMUsage class does not include fields for cached tokens or any caching-related metrics, and there is no built-in prompt caching or cached-token tracking in Dify's codebase or plugin API. All tracked usage fields (prompt/completion tokens, prices, etc.) are modeled directly from the LLM response and do not account for cache hits or cache-based pricing. If you want to track cached tokens or cache-related costs, you would need to extend the LLMUsage model and its metadata, and update the relevant logic to populate and consume these new fields in your own fork or via a plugin layer. There is no official extension point or pattern for this yet, so any such integration would be a custom modification at this time. [Reference: Dify docs and code](https://github.com/langgenius/dify-docs/blob/92cd93ba68fa0da2179f4ab78cfda383006af5cb/en/develop-plugin/features-and-specs/plugin-types/model-schema.mdx#L1173-L1189) [Reference: LLMUsage class in code](https://github.com/langgenius/dify/blob/b2cbeeae929fc1a593a04994d032dfb2b4a7fbd5/api/core/model_runtime/entities/llm_entities.py#L23-L155) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e1019f90-e74a-4ed9-a305-a9e710cb1a40?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Currently%2C%20the%20LLMUsage%20class%20does%20not%20include%20fields%20for%20cached%20tokens%20or%20any%20caching-related%20metrics%2C%20and%20there%20is%20no%20built-in%20prompt%20caching%20or%20cached-token%20tracking%20in%20Dify%27s%20codebase%20or%20plugin%20API.%20All%20tracked%20usage%20fields%20%28prompt/completion%20tokens%2C%20prices%2C%20etc.%29%20are%20modeled%20directly%20from%20the%20LLM%20response%20and%20do%20not%20account%20for%20cache%20hits%20or%20cache-based%20pricing.%20If%20you%20want%20to%20track%20cached%20tokens%20or%20cache-related%20costs%2C%20you%20would%20need%20to%20extend%20the%20LLMUsage%20model%20and%20its%20metadata%2C%20and%20update%20the%20relevant%20logic%20to%20populate%20and%20consume%20these%20new%20fields%20in%20your%20own%20fork%20or%20via%20a%20plugin%20layer.%20There%20is%20no%20official%20extension%20point%20or%20pattern%20for%20this%20yet%2C%20so%20any%20such%20integration%20would%20be%20a%20custom%20modification%20at%20this%20time.%20%20%0A%5BReference%3A%20Dify%20docs%20and%20code%5D%28https%3A//github.com/langgenius/dify-docs/blob/92cd93ba68fa0da2179f4ab78cfda383006af5cb/en/develop-plugin/features-and-specs/plugin-types/model-schema.mdx%23L1173-L1189%29%20%20%0A%5BReference%3A%20LLMUsage%20class%20in%20code%5D%28https%3A//github.com/langgenius/dify/blob/b2cbeeae929fc1a593a04994d032dfb2b4a7fbd5/api/core/model_runtime/entities/llm_entities.py%23L23-L155%29)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/30742)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21520