LibreChat/api/server/controllers/agents
Danny Avila 9a38af5875
📉 feat: Add Token Usage Tracking for Agents API Routes (#11600)
* feat: Implement token usage tracking for OpenAI and Responses controllers

- Added functionality to record token usage against user balances in OpenAIChatCompletionController and createResponse functions.
- Introduced new utility functions for managing token spending and structured token usage.
- Enhanced error handling for token recording to improve logging and debugging capabilities.
- Updated imports to include new usage tracking methods and configurations.

* test: Add unit tests for recordCollectedUsage function in usage.spec.ts

- Introduced comprehensive tests for the recordCollectedUsage function, covering various scenarios including handling empty and null collectedUsage, single and multiple usage entries, and sequential and parallel execution cases.
- Enhanced token handling tests to ensure correct calculations for both OpenAI and Anthropic formats, including cache token management.
- Improved overall test coverage for usage tracking functionality, ensuring robust validation of expected behaviors and outcomes.

* test: Add unit tests for OpenAI and Responses API controllers

- Introduced comprehensive unit tests for the OpenAIChatCompletionController and createResponse functions, focusing on the correct invocation of recordCollectedUsage for token spending.
- Enhanced tests to validate the passing of balance and transactions configuration to the recordCollectedUsage function.
- Ensured proper dependency injection of spendTokens and spendStructuredTokens in the usage recording process.
- Improved overall test coverage for token usage tracking, ensuring robust validation of expected behaviors and outcomes.
2026-02-01 21:36:51 -05:00
..
__tests__ 📉 feat: Add Token Usage Tracking for Agents API Routes (#11600) 2026-02-01 21:36:51 -05:00
callbacks.js 🦥 refactor: Event-Driven Lazy Tool Loading (#11588) 2026-02-01 08:50:57 -05:00
client.js 🦥 refactor: Event-Driven Lazy Tool Loading (#11588) 2026-02-01 08:50:57 -05:00
client.test.js 🧩 fix: Missing Memory Agent Assignment for Matching IDs (#11514) 2026-01-25 12:08:52 -05:00
errors.js 🛜 refactor: Streamline App Config Usage (#9234) 2025-08-26 12:10:18 -04:00
openai.js 📉 feat: Add Token Usage Tracking for Agents API Routes (#11600) 2026-02-01 21:36:51 -05:00
recordCollectedUsage.spec.js 🪙 refactor: Collected Usage & Anthropic Prompt Caching (#11319) 2026-01-12 23:02:08 -05:00
request.js 🏁 fix: Message Race Condition if Cancelled Early (#11462) 2026-01-21 13:57:12 -05:00
responses.js 📉 feat: Add Token Usage Tracking for Agents API Routes (#11600) 2026-02-01 21:36:51 -05:00
v1.js 🛸 feat: Remote Agent Access with External API Support (#11503) 2026-01-28 17:44:33 -05:00
v1.spec.js 🌅 fix: Agent Avatar S3 URL Refresh Pagination and Persistence (#11323) 2026-01-13 13:01:11 -05:00