Tool-call continuations reuse the ${cmd:...} provider key resolved at prompt start, so long prompts fail with an expired token
Problem
A provider whose key is ${cmd:...}, where the command prints a short-lived token (for example an OAuth access token valid for one hour), fails in the middle of a prompt once the prompt runs longer than the token's lifetime. Every tool-call continuation sends the token that was resolved when the prompt started, so the gateway answers 401 (Token Expired / expired_key).
Observed with 0.161.2 against an OpenAI- and Anthropic-compatible gateway whose tokens last one hour: prompts with tool calls that run longer than about an hour stop with 401 Token Expired. Starting a new prompt works again, because the config is resolved again at prompt start.
Cause
Read on master at 34eb0b78:
src/eca/features/chat.clj:1871resolves the config once per prompt, and the chat context keeps it.src/eca/features/chat/tool_calls.clj:1009-1016(on-tools-called!) renews login-based auth, then computes:fresh-api-keywithllm-util/provider-api-keyand that same prompt-start config.src/eca/llm_util.clj:144-157:provider-api-keyreturns the config:keyfirst, so the "fresh" key is the key resolved at prompt start.src/eca/features/login.clj:177:maybe-renew-auth-token!renews only auth that carries:expires-at, which a config key does not.
The providers already send :fresh-api-key on the continuation request (anthropic.clj:606, openai.clj:807, openai_chat.clj:695, bedrock.clj:388). config/all runs ${cmd:...} again once its cached result is older than 5 minutes (interpolation.clj:62, interpolation.clj:242-255). So a continuation that reads the key from (config/all @db*) sends a key at most 5 minutes old.
Retries of prompt! in llm-api/sync-or-async-prompt! run only before the first response (replay-safe?, llm_api.clj:620), so they reuse a key resolved moments earlier. They are not affected.
How to reproduce
- Configure a provider with
"key": "${cmd:<command that prints a token valid for N minutes>}". - Start a prompt whose tool calls keep it running for longer than N minutes, for example a shell tool call that sleeps.
- The first continuation request after the token expires fails with
401.
Proposed fix
In on-tools-called!, compute the continuation key from (config/all @db*) instead of the prompt-start config. A pull request follows.
Written by an AI agent (ECA) for @carloshernandez2.