Skip to content

Use minimum of agent config, model config, and model discovery for contextTokens/maxTokens #71136

@lykeion-dev

Description

@lykeion-dev

Problem

Currently, the context token resolution in OpenClaw follows a strict priority order:

  1. Agent config (agents.defaults.contextTokens or agents.list[].contextTokens)
  2. Provider model config (models.providers.*.models[].contextWindow)
  3. Model discovery cache (models.json)
  4. Hardcoded default (200,000)

This means if agents.defaults.contextTokens is set to 500,000 but a model only supports 128K tokens, the agent config value is used, causing API errors like:

⚠️ Context overflow — prompt too large for this model.

Proposed Solution

Instead of strict priority, use the minimum of all available values:

effectiveContextTokens = min(
  agentConfig.contextTokens ?? Infinity,
  modelConfig.contextWindow ?? Infinity,
  discoveredContextWindow ?? Infinity,
  DEFAULT_CONTEXT_TOKENS
)

This would:

  • Prevent accidental context overflow when agent config exceeds model limits
  • Allow setting a global default via agents.defaults.contextTokens without worrying about per-model limits
  • Still respect explicit per-agent overrides when they are smaller than model limits
  • Make the system safer by default

Example

{
  "agents": {
    "defaults": {
      "contextTokens": 500000
    }
  },
  "models": {
    "providers": {
      "groq": {
        "models": [
          { "id": "llama-3.3-70b-versatile", "contextWindow": 131072 }
        ]
      }
    }
  }
}

Current behavior: Agent uses 500,000 → API error
Proposed behavior: Agent uses min(500000, 131072) = 131,072 → works correctly

Additional Context

The same logic should apply to maxTokens resolution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:auth-providerAuth, provider routing, model choice, or SecretRef resolution may break.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.staleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions