From 33447ad6f2d675e1ec87375fcff9e51f2fb66600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Wed, 24 Sep 2025 19:32:16 -0700 Subject: [PATCH] BaseOpenAILLMService: include cached tokens to metrics frame --- CHANGELOG.md | 6 ++++++ src/pipecat/services/openai/base_llm.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3cd556f3..14645859d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to **Pipecat** will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Include OpenAI-based LLM services cached tokens to `MetricsFrame`. + ## [0.0.86] - 2025-09-24 ### Added diff --git a/src/pipecat/services/openai/base_llm.py b/src/pipecat/services/openai/base_llm.py index 47127b77a..5c48f86e9 100644 --- a/src/pipecat/services/openai/base_llm.py +++ b/src/pipecat/services/openai/base_llm.py @@ -337,10 +337,16 @@ class BaseOpenAILLMService(LLMService): async for chunk in chunk_stream: if chunk.usage: + cached_tokens = ( + chunk.usage.prompt_tokens_details.cached_tokens + if chunk.usage.prompt_tokens_details + else None + ) tokens = LLMTokenUsage( prompt_tokens=chunk.usage.prompt_tokens, completion_tokens=chunk.usage.completion_tokens, total_tokens=chunk.usage.total_tokens, + cache_read_input_tokens=cached_tokens, ) await self.start_llm_usage_metrics(tokens)