Skip to content

estimate_tokens re-serializes JSON tool inputs on every iteration #265

@laynepenney

Description

@laynepenney

Problem

Agent::estimate_tokens() calls message_char_count() which uses serde_json::to_string() on tool input Value objects to measure their character length. This re-serialization happens on every iteration of the chat loop, before each provider call.

Impact

For conversations with many tool calls (each containing JSON arguments and results), this creates unnecessary allocation and serialization overhead that grows with conversation length.

Proposed Fix

Options (in order of preference):

  1. Cache the char count per message: Store cached_char_count: Option<usize> on each message. Only compute once; messages are immutable after creation.
  2. Track running total: Maintain a running character count that gets incremented when messages are added. Reset on compaction.
  3. Use serde_json::to_writer with a counting writer: Avoids allocating the string entirely — just counts bytes.

Option 2 is the simplest and most efficient.

Related

PR #261 — Wire config options, context compaction, and auto-index on startup

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions