core/cli: show estimated context tokens and usage source tags

/status adds context_tokens~=(est); docs note api/estimate/mixed labels.
This commit is contained in:
2026-07-15 10:52:58 +08:00
parent 6bdf7ea283
commit d2cab4198b
2 changed files with 6 additions and 2 deletions
+2 -2
View File
@@ -58,7 +58,7 @@ Async SQLAlchemy + aiosqlite. `MemoryStore`: schema init (+ lightweight SQLite `
- **`ChatAgent`**: optional `MemoryStore` (persist on success only); `stream`; system prompt; `pending_retry_text` + `retry()`. - **`ChatAgent`**: optional `MemoryStore` (persist on success only); `stream`; system prompt; `pending_retry_text` + `retry()`.
- **`/compact`**: soft-compact tool dumps → model summary (no tools) → **new** session seeded with summary message. - **`/compact`**: soft-compact tool dumps → model summary (no tools) → **new** session seeded with summary message.
- Events: text_delta, assistant_message, tool_call/result, max_rounds, **error** (`retryable`/`source`), **cancelled** (`reason`), **usage** (`TokenUsage`). - Events: text_delta, assistant_message, tool_call/result, max_rounds, **error** (`retryable`/`source`), **cancelled** (`reason`), **usage** (`TokenUsage`).
- Usage: API `usage` from completions (stream with `include_usage`); `ChatAgent.session_usage` / `last_turn_usage`; CLI end-of-turn + `/status`. - Usage: API `usage` from completions (stream with `include_usage`); **char≈token fallback** (~4 chars/token) when omitted; `ChatAgent.session_usage` / `last_turn_usage`; CLI end-of-turn + `/status` (marks `(est)` / `(api+est)`).
- Config ``[agent]``: `system_prompt`, `max_tool_result_chars`, `parallel_tools`, `confirm_destructive`, `path_denylist`, `max_context_chars`. - Config ``[agent]``: `system_prompt`, `max_tool_result_chars`, `parallel_tools`, `confirm_destructive`, `path_denylist`, `max_context_chars`.
### Tools (`tools/`) ### Tools (`tools/`)
@@ -101,7 +101,7 @@ Basedpyright `recommended`. Ruff includes `ANN` (private return types `ANN202` i
- **Phase D (context quality)**: soft char budget, request compact, `/compact`, richer errors/cancel, workspace sessions. Context size is **char estimate** plus optional API usage when reported. - **Phase D (context quality)**: soft char budget, request compact, `/compact`, richer errors/cancel, workspace sessions. Context size is **char estimate** plus optional API usage when reported.
- **No local tokenizer stage** for now. - **No local tokenizer stage** for now.
- **Phase E**: tooling depth (grep/glob, VCS backends; prefer `edit_replace` / `edit_lineno` over model-generated patches). - **Phase E**: tooling depth (grep/glob, VCS backends; prefer `edit_replace` / `edit_lineno` over model-generated patches).
- **Phase F (providers + usage v2)**: capture response/stream `usage`**in progress / landing**; session/turn totals; `/status` + end-of-turn line. Optional later: cost, tokenizer-backed estimates. - **Phase F (providers + usage v2)**: API `usage` + char-based estimate fallback; session/turn totals; `/status` + end-of-turn. Optional later: cost, real tokenizer.
- **Phase GH**: CLI polish, hardening; then multi-tenant platform (`router/`, auth, sandboxed tools). - **Phase GH**: CLI polish, hardening; then multi-tenant platform (`router/`, auth, sandboxed tools).
## Commit messages ## Commit messages
+4
View File
@@ -60,8 +60,11 @@ def _cmd_status(state: ReplState) -> None:
pending_disp = "yes" if pending else "no" pending_disp = "yes" if pending else "no"
ctx_chars = estimate_messages_chars(state.agent.messages) ctx_chars = estimate_messages_chars(state.agent.messages)
ctx_budget = state.agent.max_context_chars ctx_budget = state.agent.max_context_chars
from plyngent.agent.usage import chars_to_tokens
session_u = state.agent.session_usage session_u = state.agent.session_usage
last_u = state.agent.last_turn_usage last_u = state.agent.last_turn_usage
ctx_est_tokens = chars_to_tokens(ctx_chars)
click.echo( click.echo(
f"provider={state.provider_name} model={state.model}\n" f"provider={state.provider_name} model={state.model}\n"
f"session={state.session_id} messages={len(state.agent.messages)} " f"session={state.session_id} messages={len(state.agent.messages)} "
@@ -69,6 +72,7 @@ def _cmd_status(state: ReplState) -> None:
f"tools={'on' if state.tools_enabled else 'off'} " f"tools={'on' if state.tools_enabled else 'off'} "
f"rounds={state.max_rounds} stream={'on' if state.agent.stream else 'off'}\n" f"rounds={state.max_rounds} stream={'on' if state.agent.stream else 'off'}\n"
f"context_chars={ctx_chars}/{ctx_budget} " f"context_chars={ctx_chars}/{ctx_budget} "
f"context_tokens~={ctx_est_tokens} (est) "
f"tool_result_max={state.agent.max_tool_result_chars}\n" f"tool_result_max={state.agent.max_tool_result_chars}\n"
f"usage_session={session_u.format_line()}\n" f"usage_session={session_u.format_line()}\n"
f"usage_last_turn={last_u.format_line()}\n" f"usage_last_turn={last_u.format_line()}\n"