Skip to content

Consistent context usage tracking for stateless executions #56

Closed

Description

@vybe

Problem

Context usage (context_used / context_max) is captured per execution but inconsistently across code paths:

  • Chat path (/api/chat): Stores context_tokens from agent session state — this is the actual context window fill level
  • Task path (routers/chat.py:749): Computes input_tokens + output_tokens — total tokens consumed
  • Scheduler path (scheduler/agent_client.py:201): Stores only input_tokens

These are three different metrics stored in the same context_used column, making the data unreliable for comparison or display.

Additionally, context is only written to the DB at execution completion (update_execution_status()). During long-running tasks (30min+), context_used is NULL.

Expected Behavior

  • All stateless execution paths (manual task, scheduled, MCP-triggered, async) should compute context_used the same way: input_tokens + output_tokens
  • This is the best approximation available for stateless sessions since there's no persistent session state to query
  • The value should be stored consistently in the schedule_executions.context_used column

Affected Code Paths

Path File Current Formula
Manual task src/backend/routers/chat.py:749 input_tokens + output_tokens
Async task src/backend/routers/chat.py:835 input_tokens + output_tokens
Scheduled execution src/scheduler/agent_client.py:201 input_tokens only
MCP chat_with_agent src/backend/services/agent_client.py:267 input_tokens only
Process engine src/backend/services/process_engine/engine/handlers/agent_task.py:105 From response.metrics.context_used

Scope

  1. Standardize formula to input_tokens + output_tokens across all stateless execution paths
  2. Verify context_max is consistently set from metadata.context_window
  3. No changes to the chat/terminal path (session-based tracking works correctly there)

🤖 Generated with Claude Code

Activity

  1. added
    priority-p1
    type-feature
    on Mar 2, 2026
  2. vybe commented on Mar 30, 2026

    @vybe
    ContributorAuthor

    Actualization (2026-03-30)

    Current state: Context tracking exists for interactive sessions but not for stateless executions.

    What exists:

    • Context window tracking in chat sessions (context_used, context_max in chat_messages)
    • Session cost tracking (cumulative cost display)
    • chat_sessions table tracks total_context_used, total_context_max

    Gap: Headless task executions (POST /task) and scheduled tasks don't consistently capture context usage. The schedule_executions table has no context columns. Context info is only available when the agent returns it in the response, which stateless tasks don't always do.

    Recommendation: Still valid. Would need the agent server to report context usage in task responses, and the backend to store it in execution records.

  3. added a commit that references this issue on Apr 14, 2026
  4. added
    status-in-progress
    on Apr 14, 2026
  5. added
    complexity-low
    on Jun 3, 2026
  6. added 2 commits that reference this issue on Jul 3, 2026
  7. removed
    status-in-progress
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p1type-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions