Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

heusalagroupbot
Copy link
Collaborator

Summary

  • Add TrimMessagesToFit to fit prompts within model window while preserving system/dev, dropping oldest non‑pinned, and truncating pinned content as needed.
  • Add PromptTokenBudget and use it to reserve space for the completion.
  • Enforce trimming in both pre‑stage and main agent request paths.
  • Add unit tests covering trimming policies and edge cases.

Context

Addresses Input tokens exceed the configured limit of 272000 tokens error in #75 by ensuring requests respect model context limits with a clear, deterministic policy.

Scope

  • Code only; no config changes required. Default window remains conservative.

Test plan

  • go test ./... passes locally.
  • New tests in internal/oai/trim_test.go validate behavior.

Closes #75.

Add TrimMessagesToFit and PromptTokenBudget to deterministically fit chat
transcripts within the model context, pinning system/developer, dropping oldest
non‑pinned, and truncating pinned content when necessary. Wire trimming into
pre‑stage and main agent loop before requests, and add unit tests.

Fixes #75.
@thejhh thejhh requested a review from Copilot August 24, 2025 15:04
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements deterministic prompt trimming to prevent token limit exceeded errors by adding a systematic approach to fit conversation transcripts within model context windows while preserving critical messages.

  • Introduces TrimMessagesToFit function with a clear policy: pin system/developer messages, drop oldest non-pinned messages first, and truncate content proportionally when needed
  • Adds PromptTokenBudget to calculate safe prompt limits considering completion token reservations
  • Enforces trimming in both main agent and pre-stage request paths to prevent API errors

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/oai/trim.go Core trimming implementation with deterministic policy for message preservation and content truncation
internal/oai/trim_test.go Comprehensive unit tests covering trimming behavior, edge cases, and policy validation
internal/oai/context_window.go Utility function to calculate prompt token budget with safety margins
cmd/agentcli/run_agent.go Integration of trimming logic in main agent request path
cmd/agentcli/prestage.go Integration of trimming logic in pre-stage request path

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@heusalagroupbot
Copy link
Collaborator Author

Status: Rebased on latest main, tests are green locally. Proceeding with review follow-ups if any.

@thejhh
Copy link
Contributor

thejhh commented Aug 24, 2025

Check out the review comment from copilot and implement it, @heusalagroupbot

1 similar comment
@thejhh
Copy link
Contributor

thejhh commented Aug 24, 2025

Check out the review comment from copilot and implement it, @heusalagroupbot

@thejhh thejhh marked this pull request as ready for review August 24, 2025 15:53
@heusalagroupbot
Copy link
Collaborator Author

Rebase: rebased cleanly onto origin/main; pushed: no; tests: passed.

@thejhh
Copy link
Contributor

thejhh commented Aug 24, 2025

Try again @heusalagroupbot. Check out the review comment from copilot review and implement it.

Copy link
Contributor

@thejhh thejhh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Input tokens exceed the configured limit of 272000 tokens

2 participants