Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4gvXXQgw1eTk5EoTT
92 lines
5.0 KiB
Java
92 lines
5.0 KiB
Java
package com.ankurm.promptcaching;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.RoundingMode;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import com.ankurm.promptcaching.support.Ask;
|
|
import com.ankurm.promptcaching.support.FakeProviders;
|
|
import com.ankurm.promptcaching.support.Transcript;
|
|
import com.ankurm.promptcaching.support.Wire;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.ai.anthropic.AnthropicCacheOptions;
|
|
import org.springframework.ai.anthropic.AnthropicCacheStrategy;
|
|
import org.springframework.ai.chat.messages.AssistantMessage;
|
|
import org.springframework.ai.chat.messages.Message;
|
|
import org.springframework.ai.chat.messages.UserMessage;
|
|
import org.springframework.ai.chat.model.ChatModel;
|
|
import org.springframework.ai.chat.model.ChatResponse;
|
|
|
|
/** A multi-turn chat: CONVERSATION_HISTORY moves one breakpoint to the newest user message every turn. */
|
|
class ConversationTest {
|
|
|
|
static final String POLICY = SupportPrompts.policy(24000);
|
|
|
|
@Test
|
|
void conversation() throws Exception {
|
|
try (FakeProviders fake = new FakeProviders();
|
|
Transcript t = new Transcript("06-conversation.txt",
|
|
"An 8-turn chat, each user turn about 1,600 characters of pasted log (claude-sonnet-5-5 prices, 5-minute cache)")) {
|
|
BigDecimal none = null;
|
|
for (AnthropicCacheStrategy s : new AnthropicCacheStrategy[] {AnthropicCacheStrategy.NONE,
|
|
AnthropicCacheStrategy.SYSTEM_ONLY, AnthropicCacheStrategy.CONVERSATION_HISTORY}) {
|
|
fake.reset();
|
|
ChatModel m = Models.anthropic(fake.url(), "claude-sonnet-5-5", AnthropicCacheOptions.builder().strategy(s).build(), false);
|
|
t.line("strategy %s", s);
|
|
List<Message> history = new ArrayList<>();
|
|
CallUsage total = CallUsage.zero();
|
|
for (int turn = 1; turn <= 8; turn++) {
|
|
history.add(new UserMessage("Turn " + turn + " log excerpt:\n" + SupportPrompts.chunk(turn)));
|
|
ChatResponse r = Ask.call(m, Ask.conversation(POLICY, history));
|
|
CallUsage u = CallUsage.from(r.getMetadata().getUsage());
|
|
total = total.plus(u);
|
|
t.line(" turn %d breakpoints=%-52s plain=%-5d read=%-6d write=%d", turn,
|
|
Wire.breakpoints(fake.last().body()), u.uncachedInput(), u.cacheRead(), u.cacheWrite());
|
|
history.add(Ask.reply(r));
|
|
fake.advance(20);
|
|
}
|
|
BigDecimal cost = Cost.dollars(Prices.SONNET_5_5, Prices.SONNET_5_5.cacheWrite5m(), total);
|
|
t.line(" 8 turns: plain=%d read=%d write=%d input+output cost $%s", total.uncachedInput(), total.cacheRead(),
|
|
total.cacheWrite(), cost.setScale(4, RoundingMode.HALF_UP));
|
|
t.blank();
|
|
if (s == AnthropicCacheStrategy.NONE) {
|
|
none = cost;
|
|
}
|
|
else if (s == AnthropicCacheStrategy.CONVERSATION_HISTORY) {
|
|
assertThat(cost).isLessThan(none);
|
|
assertThat(total.cacheRead()).isGreaterThan(total.cacheWrite());
|
|
}
|
|
}
|
|
|
|
t.line("The 20-block lookback: the same chat, but 24 messages are added between two turns");
|
|
fake.reset();
|
|
ChatModel m = Models.anthropic(fake.url(), "claude-sonnet-5-5",
|
|
AnthropicCacheOptions.builder().strategy(AnthropicCacheStrategy.CONVERSATION_HISTORY).build(), false);
|
|
List<Message> history = new ArrayList<>();
|
|
ChatResponse last = null;
|
|
for (int turn = 1; turn <= 2; turn++) {
|
|
history.add(new UserMessage("Turn " + turn + " log excerpt:\n" + SupportPrompts.chunk(turn)));
|
|
last = Ask.call(m, Ask.conversation(POLICY, history));
|
|
history.add(Ask.reply(last));
|
|
}
|
|
int normalGap = 0;
|
|
history.add(new UserMessage("Turn 3 log excerpt:\n" + SupportPrompts.chunk(3)));
|
|
CallUsage normal = CallUsage.from(Ask.call(m, Ask.conversation(POLICY, history)).getMetadata().getUsage());
|
|
t.line(" next turn, 2 messages added since the last breakpoint: read=%-6d write=%d", normal.cacheRead(), normal.cacheWrite());
|
|
history.remove(history.size() - 1);
|
|
for (int i = 0; i < 12; i++) {
|
|
history.add(new UserMessage("short note " + i));
|
|
history.add(new AssistantMessage("ok " + i));
|
|
}
|
|
history.add(new UserMessage("Turn 4 log excerpt:\n" + SupportPrompts.chunk(4)));
|
|
CallUsage jump = CallUsage.from(Ask.call(m, Ask.conversation(POLICY, history)).getMetadata().getUsage());
|
|
t.line(" next turn, 25 messages added since the last breakpoint: read=%-6d write=%d", jump.cacheRead(), jump.cacheWrite());
|
|
assertThat(normal.cacheRead()).isGreaterThan(jump.cacheRead());
|
|
assertThat(jump.cacheRead()).isBetween(5900L, 6200L);
|
|
}
|
|
}
|
|
}
|