P
Peekr Cloud
DemoAcme Agents

← Traces

agent.run

trace_id: trace_00005a · tenant: acme

7 spans·6538msok

Context growing unboundedly: 532 → 927 tokens across 3 calls

Input token counts are climbing with each LLM call in this trace (532 → 587 → 927). This is the unbounded conversation history pattern — cost and latency grow O(N²) as the session continues. At this rate, long sessions will become expensive and slow.

Fix: trim context with a rolling window or summarisation

fix.py
# Option 1: rolling window — keep only the last N messages
MAX_MESSAGES = 10
messages = messages[-MAX_MESSAGES:]

# Option 2: summarise when context exceeds a threshold
def trim_context(messages, max_tokens=3000):
    total = count_tokens(messages)
    if total <= max_tokens:
        return messages
    # Summarise the oldest half, keep the recent half verbatim
    old = messages[:-5]
    recent = messages[-5:]
    summary = llm.summarise(old)
    return [{"role": "system", "content": f"Earlier context: {summary}"}] + recent

Waterfall

agent › run

agent.run

user: u_095
openai › chat › completions › create

openai.chat.completions.create

gpt-4o791 tok0.71¢faithful 0.53user: u_095
Input

Summarize the Q3 earnings report attached. Focus on revenue, margin, and guidance.

Output

(answer to: Summarize the Q3 earnings report attache…)

openai › chat › completions › create

openai.chat.completions.create

gpt-4o684 tok0.62¢faithful 0.81user: u_095
Input

Given the customer ticket below, draft a refund response that follows policy P-204.

Output

(answer to: Given the customer ticket below, draft a…)

openai › chat › completions › create

openai.chat.completions.create

gpt-4o1.4k tok$0.012faithful 0.91user: u_095
Input

Translate the user manual section to Japanese, preserving the table structure.

Output

(answer to: Translate the user manual section to Jap…)

Tool: code_exec

tool.code_exec

user: u_095
Tool: code_exec

tool.code_exec

user: u_095
Tool: code_exec

tool.code_exec

user: u_095