Skip to content

Commit 0d34b7d

Browse files
committed
Improve LLM request logs to include headers
1 parent f53cb1f commit 0d34b7d

File tree

7 files changed

+36
-27
lines changed

7 files changed

+36
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Add workspaces to `/doctor`
6+
- Improve LLM request logs to include headers.
67

78
## 0.78.1
89

src/eca/llm_providers/anthropic.clj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,22 @@
7070
(let [url (str api-url (or url-relative-path messages-path))
7171
reason-id (str (random-uuid))
7272
oauth? (= :auth/oauth auth-type)
73+
headers (assoc-some
74+
{"anthropic-version" "2023-06-01"
75+
"Content-Type" "application/json"}
76+
"x-api-key" (when-not oauth? api-key)
77+
"Authorization" (when oauth? (str "Bearer " api-key))
78+
"anthropic-beta" (when oauth? "oauth-2025-04-20"))
7379
response* (atom nil)
7480
on-error (if on-stream
7581
on-error
7682
(fn [error-data]
7783
(llm-util/log-response logger-tag rid "response-error" body)
7884
(reset! response* error-data)))]
79-
(llm-util/log-request logger-tag rid url body)
85+
(llm-util/log-request logger-tag rid url body headers)
8086
@(http/post
8187
url
82-
{:headers (assoc-some
83-
{"anthropic-version" "2023-06-01"
84-
"Content-Type" "application/json"}
85-
"x-api-key" (when-not oauth? api-key)
86-
"Authorization" (when oauth? (str "Bearer " api-key))
87-
"anthropic-beta" (when oauth? "oauth-2025-04-20"))
88+
{:headers headers
8889
:body (json/generate-string body)
8990
:throw-exceptions? false
9091
:async? true

src/eca/llm_providers/ollama.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
(fn [error-data]
6161
(llm-util/log-response logger-tag rid "response-error" body)
6262
(reset! response* error-data)))]
63-
(llm-util/log-request logger-tag rid url body)
63+
(llm-util/log-request logger-tag rid url body {})
6464
@(http/post
6565
url
6666
{:body (json/generate-string body)

src/eca/llm_providers/openai.clj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@
3737
url (if oauth?
3838
codex-url
3939
(str api-url (or url-relative-path responses-path)))
40+
headers (assoc-some
41+
{"Authorization" (str "Bearer " api-key)
42+
"Content-Type" "application/json"}
43+
"chatgpt-account-id" (jtw-token->account-id api-key)
44+
"OpenAI-Beta" (when oauth? "responses=experimental"),
45+
"Originator" (when oauth? "codex_cli_rs")
46+
"Session-ID" (when oauth? (str (random-uuid))))
4047
on-error (if on-stream
4148
on-error
4249
(fn [error-data]
4350
(llm-util/log-response logger-tag rid "response-error" body)
4451
{:error error-data}))]
45-
(llm-util/log-request logger-tag rid url body)
52+
(llm-util/log-request logger-tag rid url body headers)
4653
@(http/post
4754
url
48-
{:headers (assoc-some
49-
{"Authorization" (str "Bearer " api-key)
50-
"Content-Type" "application/json"}
51-
"chatgpt-account-id" (jtw-token->account-id api-key)
52-
"OpenAI-Beta" (when oauth? "responses=experimental"),
53-
"Originator" (when oauth? "codex_cli_rs")
54-
"Session-ID" (when oauth? (str (random-uuid))))
55+
{:headers headers
5556
:body (json/generate-string body)
5657
:throw-exceptions? false
5758
:async? true

src/eca/llm_providers/openai_chat.clj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@
9494
on-error
9595
(fn [error-data]
9696
(llm-util/log-response logger-tag rid "response-error" error-data)
97-
{:error error-data}))]
98-
(llm-util/log-request logger-tag rid url body)
97+
{:error error-data}))
98+
headers (merge {"Authorization" (str "Bearer " api-key)
99+
"Content-Type" "application/json"}
100+
extra-headers)]
101+
(llm-util/log-request logger-tag rid url body headers)
99102
@(http/post
100103
url
101-
{:headers (merge {"Authorization" (str "Bearer " api-key)
102-
"Content-Type" "application/json"}
103-
extra-headers)
104+
{:headers headers
104105
:body (json/generate-string body)
105106
:throw-exceptions? false
106107
:async? true

src/eca/llm_util.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[clojure.string :as string]
55
[eca.config :as config]
66
[eca.logger :as logger]
7-
[eca.secrets :as secrets])
7+
[eca.secrets :as secrets]
8+
[eca.shared :as shared])
89
(:import
910
[java.io BufferedReader]
1011
[java.nio.charset StandardCharsets]
@@ -60,8 +61,11 @@
6061
""
6162
(-> result :output :contents)))
6263

63-
(defn log-request [tag rid url body]
64-
(logger/debug tag (format "[%s] Sending body: '%s', url: '%s'" rid body url)))
64+
(defn log-request [tag rid url body headers]
65+
(let [obfuscated-headers (-> headers
66+
(shared/update-some "Authorization" #(shared/obfuscate % {:preserve-num 8}))
67+
(shared/update-some "x-api-key" shared/obfuscate))]
68+
(logger/debug tag (format "[%s] Sending body: '%s', headers: '%s', url: '%s'" rid body obfuscated-headers url))))
6569

6670
(defn log-response [tag rid event data]
6771
(logger/debug tag (format "[%s] %s %s" rid (or event "") data)))

src/eca/shared.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@
147147
"Obfuscate all but first 3 and last 3 characters of a string, minimum 5 characters.
148148
If the string is 4 characters or less, obfuscate all characters.
149149
Replace the middle part with asterisks, but always at least 5 asterisks."
150-
[s]
150+
[s & {:keys [preserve-num]
151+
:or {preserve-num 3}}]
151152
(when s
152153
(string/replace
153154
(if (<= (count s) 4)
154155
(apply str (repeat (count s) "*"))
155-
(str (subs s 0 3)
156+
(str (subs s 0 preserve-num)
156157
(apply str (repeat (- (count s) 4) "*"))
157-
(subs s (- (count s) 3))))
158+
(subs s (- (count s) preserve-num))))
158159
(string/join "" (repeat (- (count s) 4) "*")) "*****")))
159160

160161
(defn normalize-model-name [model]

0 commit comments

Comments
 (0)