Skip to content

LLM benchmark: Bedrock + AgentCore Web Search, prompt caching, reliability fixes, history analysis - #25

Merged
shuva10v merged 11 commits into
mainfrom
feat/llm-benchmark-bedrock-agentcore
Oct 6, 2026
Merged

shuva10v merged 11 commits into
mainfrom
feat/llm-benchmark-bedrock-agentcore

Conversation

@shuva10v

@shuva10v shuva10v commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds Amazon Bedrock models and Web Search on Amazon Bedrock AgentCore to the LLM benchmark agent, plus fixes for the reliability and cost problems found while running Claude Fable 5.1 and Nova 2 Lite live (rounds 167–176), and tooling for a historical post-mortem of rounds 2–95.

The provider is a config switch alongside OpenRouter, not a separate module: prompt, tools and the on-chain flow stay shared, so models remain comparable. Existing OpenRouter agents keep their current behavior apart from the changes listed under Behavior changes for existing agents.

Providers and search

  • LLM_PROVIDER=openrouter|bedrock. The Bedrock path uses @ai-sdk/amazon-bedrock (Converse API) with the default AWS credential chain.
  • SEARCH_PROVIDER=tavily|agentcore|none, auto-detected. lib/agentcore.mjs is a minimal MCP client for AgentCore Gateway (SigV4 or JWT) that auto-discovers the <target>___WebSearch tool. Results are normalized to the same shape as Tavily.
  • Web search preflight at startup: DRY_RUN fails hard; live runs continue without search so reveals are never blocked.
  • SEARCH_EXCLUDE_DOMAINS (default polymarket.com) applies to every backend. It drops only the exact host and www., so xtracker.polymarket.com (a resolution source) is kept.

Cost

  • Bedrock prompt caching, per model family: Claude caches the prompt plus the newest tool results; Nova caches the prompt only, because it rejects cache points in tool-result messages. The tool loop now runs one step at a time so cache points can move.
  • OpenRouter prompt caching for anthropic/* models through a top-level cache_control, injected by a fetch wrapper because the 0.0.6 SDK has no caching support. Live Fable 5.1 run: 308k of 375k prompt tokens read from cache, ~$1.22 vs ~$4.06 without caching.
  • The loop stops right after submitPredictions instead of paying for a closing summary.

Reliability

  • Current date in the prompt. Without it, a dry run searched for "October 2024".
  • Per-wallet run lock (state/run-<address>.lock). In round 169 a slow Fable call outlasted the 2-minute cron and a second process predicted the same round.
  • Deadline guard: the LLM loop stops 45s before the commit deadline, before each step and mid-call. It is reported as Out of time even when the SDK wraps the abort as "Failed to process successful response" (round 172).
  • Submit reminder: if a model answers in text without calling submitPredictions (Nova, round 176), the agent asks it to submit, up to 2 times.
  • Cache fallback: if a provider rejects a cache marker, the step is retried uncached.

Analysis and post-processing

  • tools/export-history.mjs: scored rounds with market metadata, outcomes, per-market Brier/alpha (verified against on-chain scores) and the posted reasoning.
  • tools/analyze-history.mjs: alpha by period, category, price and deviation; shrinkage; ensemble; calibration; confident calls; split-half checks.
  • Findings for rounds 2–95 (4 LLM agents, 1,744 predictions): on average the agents roughly match the market (−0.0044 alpha per market). Confident "already decided" calls are their real edge (+3.9 total, 80% right), while moderate and opinion deviations lose (−11.7 total).
  • SHRINK_TO_MARKET=k post-processing (default 1 = off). k=0.5 turned every agent's alpha positive in both halves of the data.
  • Reasoning logs: every prediction run (including DRY_RUN) saves predictions, per-market reasoning, the full tool trace, token and cache usage, and raw vs committed forecasts to state/reasoning/.

Behavior changes for existing agents

  • Prompt now includes the current date. This changes the prompt for every model.
  • polymarket.com search results are excluded by default. Set SEARCH_EXCLUDE_DOMAINS= (empty) to restore the old behavior.
  • OpenRouter Claude models get prompt caching (cheaper, same output). Disable with OPENROUTER_PROMPT_CACHE=off.
  • ai now requires ^4.3.0. Run npm install after pulling.
  • agents/*/state/ and tools/out/ are now gitignored.

Testing

  • Offline tests with mocked Bedrock, OpenRouter and AgentCore Gateway endpoints covering: SigV4/JWT, JSON/SSE responses, tool discovery, cache-point placement per model, cache rejection fallback, OpenRouter cache_control and usage capture, deadline aborts (including mid-body), the run lock, the submit reminder and the shrink function. Each live failure (Nova cache rejection, wrapped abort, text answer) was reproduced in a test before fixing.
  • Live on the server: Bedrock Sonnet 4.5 dry runs on round 167 with AgentCore search, then Fable 5.1 (OpenRouter) and Nova 2 Lite (Bedrock) committing in rounds 169+.

Not in this PR

  • Gasless commits through the relayer: agent wallets still pay POL.
  • Retrying a round whose prediction failed early: the round is still dropped.
  • The LLM post-mortem / learning loop (discussed separately).

🤖 Generated with Claude Code

shuva10v and others added 11 commits September 30, 2026 19:00
- LLM_PROVIDER=openrouter|bedrock selects the AI SDK provider; Bedrock uses
  @ai-sdk/amazon-bedrock (Converse API) with the default AWS credential chain
- searchWeb is now backed by a pluggable search backend
  (SEARCH_PROVIDER=tavily|agentcore|none, auto-detected from env)
- AgentCore backend: minimal MCP client for AgentCore Gateway (SigV4 or JWT
  inbound auth), auto-discovers the `<target>___WebSearch` tool name and
  normalizes results to the same shape as Tavily
- Prompt, tools and on-chain flow are unchanged, so existing OpenRouter
  deployments behave the same
- README: env vars, gateway setup, fairness note

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- Every prediction run (incl. DRY_RUN) writes predictions, per-market
  reasoning, full tool trace (search queries + results) and token usage to
  state/reasoning/<slug>-round-<id>-<ts>[-dryrun].json
- DRY_RUN also prints tool calls and per-market reasoning to the console
- gitignore agents/*/state/ (reveal queues contain salts)
- README: correct the reasoning-storage section — the relayer only receives
  the per-market reasoning strings, not the trace

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- Prompt now states the current UTC date/time. A Bedrock dry run on round 167
  showed the model searching for "October 2024" because it assumed its
  training-era date. Applies to all models/providers.
- AgentCore web search preflight (tools/list) at startup in predict/all mode
  and DRY_RUN. DRY_RUN fails hard; live runs log an error and predict without
  search so reveals in the same run are never blocked.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…results

- Drive the tool loop one step at a time so cache points can be placed on the
  prompt and the two newest tool-result messages (Bedrock max 4). A dry run on
  round 167 used 205k prompt tokens across 8 steps, mostly re-sent search
  snippets. Enabled automatically for Claude/Nova (BEDROCK_PROMPT_CACHE).
- Stop right after submitPredictions instead of paying for a closing summary.
- Report cache read/write tokens per step and in the usage log line.
- SEARCH_EXCLUDE_DOMAINS (default polymarket.com) drops results hosted exactly
  on excluded domains for every search backend; subdomains such as
  xtracker.polymarket.com (a resolution source) are kept. Over-fetches 3
  results so the model still gets 5.
- ai >= 4.3 (message-level providerOptions).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- export-history.mjs: scored rounds from the subgraph with market metadata
  (relayer /polymarket proxy), outcomes, per-agent predictions, per-market
  Brier/alpha (verified against on-chain scores) and posted reasoning
- analyze-history.mjs: alpha by period/category/price/deviation, shrinkage
  toward the market, LLM ensemble, calibration, confident "already decided"
  calls, split-half check of post-processing rules, largest losses
- gitignore tools/out/

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
final = market + k·(model − market), using the YES price the model saw.
Default k=1 (off). Auto-resolved markets are never shrunk; if the market
price is unknown the forecast is left unchanged.

Rounds 2–95 analysis: the benchmark LLMs lose to the market on moderate and
opinion deviations; k=0.5 turned all four agents' alpha positive, in both
halves of the data (-0.0044 → +0.0029 per market pooled).

The reasoning log records modelPredictions, marketPrices and the factor;
per-market reasoning keeps probabilityBps (committed) and
modelProbabilityBps (raw).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Nova 2 Lite failed round 168 with "Malformed input request:
#/messages/2/content/6: extraneous key [cachePoint] is not permitted".

- Cache policy by family: Claude = prompt + newest tool results,
  Nova = prompt only, others = none
- If Bedrock rejects a cache point anyway, retry the step uncached and keep
  caching off for the rest of the run, so a caching problem can't cost a round

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… deadline

Round 169: a Fable call started at 04:56 was still running when the 04:58
cron fired, so a second process predicted the same round (double LLM cost).
The first committed 28s before the deadline; the second's commit reverted in
simulation ("Already committed").

- state/run-<address>.lock: one run at a time per wallet; skips while the
  holder is alive, takes over a lock from a dead process, removed on exit
- getPredictions(deadline): checked before each step and enforced mid-call
  with an abort signal; the agent reserves 45s before the commit deadline

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Fable 5.1 on OpenRouter re-sent the whole conversation each step: 255k prompt
tokens over 8 steps in round 169, ~$3.14/round. With caching, a replay of that
trace comes to ~$1.17 (reads $0.25/M vs $10/M input).

- fetch wrapper (the 0.0.6 SDK has no caching support) adds top-level
  cache_control for anthropic/* models and records
  usage.prompt_tokens_details (cached_tokens / cache_write_tokens) per step
- other models' requests are unchanged; OPENROUTER_PROMPT_CACHE=on|off
- cache rejections are detected in message or responseBody and the step is
  retried uncached

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…rapped

Round 172 (Fable): the deadline guard fired at 17:59:15, exactly 45s before
the 18:00 deadline, while OpenRouter's response body was being read. The
abort surfaced as an undici "terminated" error, which the SDK wraps as
"Failed to process successful response", so it was logged as an opaque
failure. Check our own abort signal instead of the error type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Round 176 (Nova 2 Lite): after 5 steps the model wrote its predictions as
text ("Prediction: 8450 bps ...") and stopped without calling
submitPredictions, so the round was lost.

If a step ends without a tool call and no predictions were submitted, append
a short user message asking for the submitPredictions call, up to 2 times,
within the same deadline guard. Forcing tool_choice is not an option (newer
Claude models reject it) and parsing free text would be fragile.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@shuva10v
shuva10v merged commit be4a316 into main Oct 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant