noteflowai/ai-chat-for-amazon-bedrock · 1.74.1 · 2026-11-18

fix: wp ai-chat-bedrock usage labels chat requests whose token usage Bedrock did not fully report

Before this fix, a chat answer for which Bedrock reported no input or output token count was added to the totals as 0 tokens, and nothing recorded that a count was missing. In 1.74.1, Usage::record() still counts such a request and adds whatever counts were reported. It also adds 1 to a per-day counter, usage_unknown_requests. A chat answer counts as unknown when either count is missing or is not a number. A real 0 counts as reported and is not marked. Embedding, rerank and speech records are never marked. prune() keeps the counter for the same retention period as the other counters. Usage::totals() also returns the sum of usage_unknown_requests over the window. When that sum is above 0, the daily view of wp ai-chat-bedrock usage prints one extra line directly after the Total line, saying the token totals are a lower bound. The count is capped at the window's request total. The line has no denominator, so requests recorded before the upgrade are never shown as measured. The --by-model output, the exit status and the Total line format are unchanged. The version is 1.74.1, with a changelog entry and an upgrade notice. Controller results for commit 0871b38: before the fix, the controller ran tests/test_feature_56689b30e4cc.php and 7 of its 8 cases failed (exit 1). On commit 0871b38, the same test passed all 8 cases. bin/prerelease.sh passed, reporting 'Ready to package and publish 1.74.1'. tests/tooling.php passed. bin/build-package.py built a 1.74.1 package of 136 files. I did not run any of these commands myself.

Use it

Prerequisites: a WordPress site running plugin 1.74.1, with WP-CLI. For the synthetic demo below, use a disposable staging site, because it writes to the plugin's real usage records and adds to today's request count.

1. Normal use (no input file needed):
wp ai-chat-bedrock usage --days=7

2. Synthetic demo (staging only). Record one chat answer with complete counts and two with incomplete counts:
wp eval "AI_Chat_Bedrock_Usage::record( array( 'input_tokens' => 120, 'output_tokens' => 40 ), 'amazon.nova-lite-v1:0' ); AI_Chat_Bedrock_Usage::record( array(), 'amazon.nova-lite-v1:0' ); AI_Chat_Bedrock_Usage::record( array( 'input_tokens' => 5 ), 'amazon.nova-lite-v1:0' );"
wp ai-chat-bedrock usage --days=7

On a site with no earlier usage since the upgrade, the output includes:
Total: 3 requests, 125 input tokens, 40 output tokens.
Token usage unknown for 2 requests in this window: Bedrock did not report complete token counts for them, so the token totals above are a lower bound.
The report's other lines follow unchanged. Usage recorded earlier in the window adds to both numbers. With one unknown request the line reads '1 request in this window'. With none, the line is left out. wp ai-chat-bedrock usage --by-model is unchanged.

Scope

The tests seed synthetic stored records and run the real CLI class against WP-CLI stubs. They did not use live Bedrock responses or a real WordPress install. prerelease.sh skipped the official Plugin Check, the live integration check and the translation template comparison, because no --wp-cli was given. It also skipped core-ai-library.php because AICFAB_AI_CLIENT was not set. I did not check which Bedrock models or stream paths actually leave out usage. Requests recorded before 1.74.1 were never classified, so they are not counted as unknown, and the line has no denominator. The admin metrics dashboard and the --by-model table still show missing counts as 0 tokens. Embedding, rerank and speech usage are not classified. This change is CLI-only. prerelease.sh ran the existing JavaScript script tests (admin, analytics, early-taps, public and speech), but those cover other features. No screenshot or visual review of this change exists, and none applies to its CLI output. The readme changelog is at 4963 of its 5000-character limit, so the next entry will need older entries moved out.

Upgrade and rollback

Upgrade with the normal plugin update to 1.74.1. No migration is needed. Day entries from before the upgrade have no usage_unknown_requests key and count as 0. New chat answers start the counter. Stored token, request, cache, failure and latency data stay as they are. Scripts that parse the Total line keep working, because its format is unchanged. A script that reads the line after Total may now find the new unknown line there. Rollback: install 1.74.0. Its prune() rebuilds each day from the keys it knows, so the next write drops usage_unknown_requests and keeps every other counter. Otherwise, the counter is pruned with its day after RETENTION_DAYS, like the other counters.

Source commit: 0209ca74b770282ce72e6b728dc61c43c7c5b3a6