MCPcopy Index your code
hub / github.com/coder/coder / addAnthropicPromptCaching

Function addAnthropicPromptCaching

coderd/x/chatd/chatloop/chatloop.go:1932–1956  ·  view source on GitHub ↗

addAnthropicPromptCaching mutates messages in-place, setting ProviderOptions for Anthropic prompt caching on the last system message and the final two messages.

(messages []fantasy.Message)

Source from the content-addressed store, hash-verified

1930// ProviderOptions for Anthropic prompt caching on the last system
1931// message and the final two messages.
1932func addAnthropicPromptCaching(messages []fantasy.Message) {
1933 for i := range messages {
1934 messages[i].ProviderOptions = nil
1935 }
1936
1937 providerOption := fantasy.ProviderOptions{
1938 fantasyanthropic.Name: &fantasyanthropic.ProviderCacheControlOptions{
1939 CacheControl: fantasyanthropic.CacheControl{Type: "ephemeral"},
1940 },
1941 }
1942
1943 lastSystemRoleIdx := -1
1944 systemMessageUpdated := false
1945 for i, msg := range messages {
1946 if msg.Role == fantasy.MessageRoleSystem {
1947 lastSystemRoleIdx = i
1948 } else if !systemMessageUpdated && lastSystemRoleIdx >= 0 {
1949 messages[lastSystemRoleIdx].ProviderOptions = providerOption
1950 systemMessageUpdated = true
1951 }
1952 if i > len(messages)-3 {
1953 messages[i].ProviderOptions = providerOption
1954 }
1955 }
1956}
1957
1958// recordToolResultTimestamp lazily initializes the
1959// toolResultCreatedAt map on the stepResult and records

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected