TurnGEO / tests /test_usage.py
janwikadyn's picture
Initial commit: TurnGEO evaluation harness
d710584
Raw History Blame Contribute Delete
1.63 kB
"""`usage.message_usage` on the usage metadata the LangChain clients actually produce."""
import pytest
from langchain_core.messages import AIMessage
from turngeo import usage
def test_anthropic_cache_writes_reported_per_ttl_are_counted():
anthropic_types = pytest.importorskip("anthropic.types")
chat_models = pytest.importorskip("langchain_anthropic.chat_models")
raw = anthropic_types.Usage(
input_tokens=50, output_tokens=10, cache_read_input_tokens=200, cache_creation_input_tokens=1000,
cache_creation={"ephemeral_5m_input_tokens": 1000, "ephemeral_1h_input_tokens": 0},
)
metadata = chat_models._create_usage_metadata(raw)
assert metadata["input_token_details"]["cache_creation"] == 0 # the client moves writes to the TTL keys
counts = usage.message_usage(AIMessage(content="x", usage_metadata=metadata))
assert counts == dict(input_tokens=1250, output_tokens=10, cache_read_tokens=200, cache_write_tokens=1000, estimated=False)
def test_a_plain_cache_creation_count_is_used_as_is():
metadata = {"input_tokens": 100, "output_tokens": 5, "total_tokens": 105,
"input_token_details": {"cache_read": 10, "cache_creation": 40}}
counts = usage.message_usage(AIMessage(content="x", usage_metadata=metadata))
assert (counts["cache_read_tokens"], counts["cache_write_tokens"]) == (10, 40)
def test_a_response_without_usage_is_estimated_not_zero():
counts = usage.message_usage(AIMessage(content="y" * 40), prompt_chars=400)
assert counts == dict(input_tokens=100, output_tokens=10, cache_read_tokens=0, cache_write_tokens=0, estimated=True)