Skip to content

Commit ad2e9d5

Browse files
committed
Add support for prompt caching in anthropic
1 parent 25499f4 commit ad2e9d5

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Add MCP/tool support for ollama models.
77
- Improve ollama integration only requiring `ollama serve` to be running.
88
- Improve chat history and context passed to all LLM providers.
9+
- Add support for prompt caching for Anthropic models.
910

1011
## 0.1.0
1112

docs/capabilities.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ ECA support built-in tools to avoid user extra installation and configuration, t
88

99
Provides access to filesystem under workspace root, listing and reading files and directories.
1010

11+
- ''
12+
1113
## Supported LLM models and capaibilities
1214

13-
| model | MCP / tools | thinking/reasioning |
14-
|-----------|-------------|---------------------|
15-
| OpenAI || X |
16-
| Anthropic || X |
17-
| Ollama || X |
15+
| model | MCP / tools | thinking/reasioning | prompt caching |
16+
|-----------|-------------|---------------------|----------------|
17+
| OpenAI || X | X |
18+
| Anthropic || X ||
19+
| Ollama || X | X |
1820

1921
### OpenAI
2022

src/eca/llm_providers/anthropic.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
:input_schema (:parameters tool))) tools)
2727
web-search (conj {:type "web_search_20250305"
2828
:name "web_search"
29-
:max_uses 10})))
29+
:max_uses 10
30+
:cache_control {:type "ephemeral"}})))
3031

3132
(defn ^:private base-request! [{:keys [rid body api-key content-block* on-error on-response]}]
3233
(let [api-key (or api-key
@@ -73,7 +74,8 @@
7374
:content (llm-util/stringfy-tool-result content)}]}
7475
msg))
7576
past-messages)
76-
{:role "user" :content user-prompt}))
77+
;; TODO add cache_control to last non thinking message
78+
{:role "user" :content user-prompt :cache_control {:type "ephemeral"}}))
7779

7880
(defn completion!
7981
[{:keys [model user-prompt temperature context max-tokens
@@ -89,7 +91,7 @@
8991
;; TODO support :thinking
9092
:stream true
9193
:tools (->tools tools web-search)
92-
:system context}
94+
:system [{:type "text" :text context :cache_control {:type "ephemeral"}}]}
9395
on-response-fn
9496
(fn handle-response [event data content-block*]
9597
(case event

test/eca/llm_providers/anthropic_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
(deftest ->messages-with-history-test
88
(testing "no previous history"
99
(is (match?
10-
[{:role "user" :content "Hey"}]
10+
[{:role "user" :content "Hey" :cache_control {:type "ephemeral"}}]
1111
(#'llm-providers.anthropic/->messages-with-history [] "Hey"))))
1212
(testing "With basic text history"
1313
(is (match?
1414
[{:role "user" :content "Count with me: 1"}
1515
{:role "assistant" :content "2"}
16-
{:role "user" :content "3"}]
16+
{:role "user" :content "3" :cache_control {:type "ephemeral"}}]
1717
(#'llm-providers.anthropic/->messages-with-history
1818
[{:role "user" :content "Count with me: 1"}
1919
{:role "assistant" :content "2"}]
@@ -30,7 +30,7 @@
3030
:tool-use-id "call-1"
3131
:content "Allowed directories: /foo/bar\n"}]}
3232
{:role "assistant" :content "I see /foo/bar"}
33-
{:role "user" :content "Thanks"}]
33+
{:role "user" :content "Thanks" :cache_control {:type "ephemeral"}}]
3434
(#'llm-providers.anthropic/->messages-with-history
3535
[{:role "user" :content "List the files you are allowed"}
3636
{:role "assistant" :content "Ok!"}

0 commit comments

Comments
 (0)