Skip to content

Conversation

@MshariAlaeena
Copy link
Contributor

Description:
Adds support for prompt caching usage metadata in ChatGroq. The integration now captures cached token information from the Groq API response and includes it in the input_token_details field of the usage_metadata.

Changes:

  • Created new _create_usage_metadata() helper function to centralize usage metadata creation logic
  • Extracts cached_tokens from prompt_tokens_details in API responses and maps to input_token_details.cache_read
  • Integrated the helper function in both streaming (_convert_chunk_to_message_chunk) and non-streaming (_create_chat_result) code paths
  • Added comprehensive unit tests to verify caching metadata handling and backward compatibility

This enables users to monitor prompt caching effectiveness when using Groq models with prompt caching enabled.

Issue: N/A

Dependencies: None

@github-actions github-actions bot added integration Related to a provider partner package integration feature labels Oct 28, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 28, 2025

CodSpeed Performance Report

Merging #33708 will not alter performance

Comparing MshariAlaeena:malaeena/add-prompt-caching-usage-fields (89b2f65) with master (3c49257)

Summary

✅ 1 untouched
⏩ 33 skipped1

Footnotes

  1. 33 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

output_tokens = groq_token_usage.get("completion_tokens") or 0
total_tokens = groq_token_usage.get("total_tokens") or input_tokens + output_tokens
input_token_details: dict = {
"cache_read": (groq_token_usage.get("prompt_tokens_details") or {}).get(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find anywhere in Groq's docs where prompt_token_details are returned?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be input_tokens_details

Copy link
Contributor Author

@MshariAlaeena MshariAlaeena Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking, It’s actually documented here:
https://console.groq.com/docs/prompt-caching#tracking-cache-usage

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MshariAlaeena I think that might be wrong? See Slack

@github-actions github-actions bot added the groq label Nov 7, 2025
@mdrxy mdrxy merged commit 9383b78 into langchain-ai:master Nov 7, 2025
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature groq integration Related to a provider partner package integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants