Class AgentUsageStats

java.lang.Object
io.jenkins.plugins.aiagentjob.AgentUsageStats
All Implemented Interfaces:
Serializable

public final class AgentUsageStats extends Object implements Serializable
Normalized token usage and cost statistics extracted from AI agent JSONL logs. Aggregates values across all lines in the log (multiple turns, partial results, etc.) so the final object reflects totals for the entire session.

Agent-specific extraction logic is delegated to AiAgentStatsExtractor implementations, which are provided by each AiAgentTypeHandler via AiAgentTypeHandler.getStatsExtractor(). Shared/common extraction (system init, result lines) is handled by the base class as a fallback.

See Also:
  • Constructor Details

    • AgentUsageStats

      public AgentUsageStats()
  • Method Details

    • getInputTokens

      public long getInputTokens()
    • getOutputTokens

      public long getOutputTokens()
    • getCacheReadTokens

      public long getCacheReadTokens()
    • getCacheWriteTokens

      public long getCacheWriteTokens()
    • getTotalTokens

      public long getTotalTokens()
    • getReasoningTokens

      public long getReasoningTokens()
    • getCostUsd

      public double getCostUsd()
    • getCostDisplay

      public String getCostDisplay()
      Formatted cost string like "$0.30" or empty if no cost data.
    • getDurationMs

      public long getDurationMs()
    • getDurationDisplay

      public String getDurationDisplay()
      Formatted duration like "4.5s" or "2m 15s".
    • getApiDurationMs

      public long getApiDurationMs()
    • getNumTurns

      public int getNumTurns()
    • getToolCalls

      public int getToolCalls()
    • getDetectedModel

      public String getDetectedModel()
      Model name detected from system init or result lines, empty if not found.
    • getInputTokensDisplay

      public String getInputTokensDisplay()
      Formats a token count with comma grouping (e.g., "103,854").
    • getOutputTokensDisplay

      public String getOutputTokensDisplay()
    • getCacheReadTokensDisplay

      public String getCacheReadTokensDisplay()
    • getCacheWriteTokensDisplay

      public String getCacheWriteTokensDisplay()
    • getTotalTokensDisplay

      public String getTotalTokensDisplay()
    • getReasoningTokensDisplay

      public String getReasoningTokensDisplay()
    • hasData

      public boolean hasData()
      Returns true if any meaningful data was extracted.
    • addInputTokens

      public void addInputTokens(long value)
      Accumulate input tokens (takes the max of current and new value).
    • addOutputTokens

      public void addOutputTokens(long value)
      Accumulate output tokens (takes the max of current and new value).
    • addCacheReadTokens

      public void addCacheReadTokens(long value)
      Accumulate cache read tokens (takes the max of current and new value).
    • addCacheWriteTokens

      public void addCacheWriteTokens(long value)
      Accumulate cache write tokens (takes the max of current and new value).
    • addTotalTokens

      public void addTotalTokens(long value)
      Accumulate total tokens (takes the max of current and new value).
    • addReasoningTokens

      public void addReasoningTokens(long value)
      Accumulate reasoning tokens (takes the max of current and new value).
    • incrementInputTokens

      public void incrementInputTokens(long value)
      Increment input tokens (additive, for multi-step agents like OpenCode).
    • incrementOutputTokens

      public void incrementOutputTokens(long value)
      Increment output tokens (additive).
    • incrementReasoningTokens

      public void incrementReasoningTokens(long value)
      Increment reasoning tokens (additive).
    • incrementTotalTokens

      public void incrementTotalTokens(long value)
      Increment total tokens (additive).
    • incrementCacheReadTokens

      public void incrementCacheReadTokens(long value)
      Increment cache read tokens (additive).
    • incrementCacheWriteTokens

      public void incrementCacheWriteTokens(long value)
      Increment cache write tokens (additive).
    • incrementCostUsd

      public void incrementCostUsd(double value)
      Increment cost (additive).
    • addCostUsd

      public void addCostUsd(double value)
      Set cost (takes the max of current and new value).
    • addDurationMs

      public void addDurationMs(long value)
      Set duration (takes the max of current and new value).
    • addApiDurationMs

      public void addApiDurationMs(long value)
      Set API duration (takes the max of current and new value).
    • addNumTurns

      public void addNumTurns(int value)
      Set number of turns (takes the max of current and new value).
    • addToolCalls

      public void addToolCalls(int value)
      Set tool call count (takes the max of current and new value).
    • setDetectedModelIfEmpty

      public void setDetectedModelIfEmpty(String model)
      Set the detected model name if not already set.
    • fromLogFile

      public static AgentUsageStats fromLogFile(File logFile) throws IOException
      Parses the entire JSONL log file and returns aggregated stats. Each line that contains usage or stats information contributes to the totals.
      Throws:
      IOException
    • fromLogFile

      public static AgentUsageStats fromLogFile(File logFile, AiAgentStatsExtractor extractor) throws IOException
      Parses the entire JSONL log file using the given extractor and returns aggregated stats. The extractor is tried first for each line; if it returns false, the shared extractor handles the line.
      Throws:
      IOException
    • extractFrom

      public void extractFrom(net.sf.json.JSONObject json, AiAgentStatsExtractor extractor)
      Extracts stats using the given extractor (if any), falling back to shared extraction.
    • extractFrom

      public void extractFrom(net.sf.json.JSONObject json)
      Extracts stats from a single JSON line without any agent-specific extractor.
    • extractResultStats

      public void extractResultStats(net.sf.json.JSONObject json)
      Extracts stats from a "result" event (Claude Code / Gemini / Cursor shared structure).
    • accumulateUsage

      public void accumulateUsage(net.sf.json.JSONObject usage)
      Accumulates token counts from a standard usage object.