Skip to content

refactor(harness): unify context token accounting and charge provider default output schemas - #3570

Open
dargoner wants to merge 2 commits into
agentscope-ai:mainfrom
dargoner:refactor/unify-context-token-accounting
Open

dargoner wants to merge 2 commits into
agentscope-ai:mainfrom
dargoner:refactor/unify-context-token-accounting

Conversation

@dargoner

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (based on main @ 5f7f8e6)

Description

Background

Context-budget accounting was spread across several call sites with different conventions: the final-request budget ignored provider-level default output schemas (Ollama format, OpenAI/OpenAI-Responses response_format, and the Anthropic/DashScope/Gemini equivalents) that the adapters actually send on the wire; media charging was duplicated between estimators; and the pruning pass accumulated token counts in int, which can overflow on very large histories.

Changes

  • Unified accounting service (TokenAccountingService, MediaTokenEstimator): a single place that prices a request (text + tools + media + output reservation + response-format schemas), reused by budget checks, compaction and tests.
  • Effective model options (Model, SessionModels, ReActAgent, HarnessContextBuilder): models expose getDefaultOptions() / resolveOptions(); the context builder budgets what the provider will actually send (explicit options merged over provider defaults). Default response schemas are now charged for OpenAI, OpenAI official (Responses), DashScope, Anthropic, Gemini and Ollama, with format semantics aligned to the provider's effective wire format (including JSON-representation detection for Ollama object schemas).
  • Prune accounting hardening (ConversationCompactor, TokenCounterUtil): accumulation widened to long and per-result accounting aligned with the retained/pruned split.
  • Regression tests that exercise the real adapter classes so default-schema pricing cannot silently drift.

Tests

  • New: ContextBudgetRegressionTest (9), ProviderDefaultSchemaBudgetTest (21), TokenAccountingServiceTest (10).
  • Updated: HarnessContextBuilderTest 8 → 12, ConversationCompactorTest extended.
  • Full 13-module reactor build after rebase onto main: 6433 tests, 0 failures / 0 errors (43 environment skips).
  • mvn spotless:check passes.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.) — N/A for this change (internal accounting; Javadoc covers the new APIs)
  • Code is ready for review

@dargoner
dargoner force-pushed the refactor/unify-context-token-accounting branch from f1ee22c to ae77c76 Compare October 29, 2026 12:07
@jujn jujn added the P2 重要不紧急 label Nov 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants