Skip to content

feat: improve token counting and add brevity pattern#7

Open
PatrickRuddiman wants to merge 3 commits intomainfrom
l6macz-codex/check-context-length-before-request
Open

feat: improve token counting and add brevity pattern#7
PatrickRuddiman wants to merge 3 commits intomainfrom
l6macz-codex/check-context-length-before-request

Conversation

@PatrickRuddiman
Copy link
Owner

@PatrickRuddiman PatrickRuddiman commented Jun 29, 2025

This pull request introduces a new brevity pattern to handle context overflow scenarios, improves token estimation accuracy, and refactors the codebase to support these changes. The most important updates include defining the brevity pattern, implementing token estimation using TiktokenSharp, and enhancing the chunk summarization logic in OpenAIService.

New Pattern for Context Overflow:

  • Constants/FabricPatterns.cs and Constants/PatternNames.cs: Added a new constant BrevityPattern to define the pattern used for summarization when context overflow occurs. [1] [2]
  • patterns/brief_chunk_summary/system.md: Introduced a new pattern file that outlines the purpose, steps, and output for the brevity summarization process.

Token Estimation Improvements:

Enhanced Chunk Summarization Logic:

  • Services/OpenAIService.cs: Introduced logic to handle context overflow by re-chunking summaries and using the new BrevityPattern for condensed summarization. Added a constant MaxContextTokens to define the token limit. [1] [2]

Documentation Update:

  • README.md: Documented the new brief_chunk_summary pattern and its usage for handling overflowing contexts.

@PatrickRuddiman PatrickRuddiman requested a review from Copilot June 29, 2025 15:41
Copy link
Contributor

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 a new brevity pattern for handling context overflow scenarios, refactors token estimation to improve accuracy using TiktokenSharp, and enhances the chunk summarization logic in the OpenAIService.

  • Introduces a new constant and pattern file for brevity summarization.
  • Implements token estimation improvements through a dedicated TokenHelper class and integrates it into other services.
  • Refactors OpenAIService to re-chunk messages when context tokens exceed the limit.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
patterns/brief_chunk_summary/system.md Adds a new pattern file describing the brevity summarization process.
WriteCommit.csproj Adds dependency to TiktokenSharp for enhanced token estimation.
Services/TokenHelper.cs Implements token estimation using TiktokenSharp with fallback logic.
Services/SemanticCoherenceAnalyzer.cs Refactors token estimation to use the new TokenHelper for better accuracy.
Services/OpenAIService.cs Enhances chunk summarization logic with re-chunking of messages for context overflow.
README.md Updates documentation to include usage of the brevity pattern.
Constants/PatternNames.cs Adds a constant for the new brevity pattern.
Constants/FabricPatterns.cs Adds a constant for the new brevity pattern.


var groupedSummaries = new List<string>();
var currentGroup = new List<string>();
var currentTokens = TokenHelper.EstimateTokens(systemPrompt, model);
Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

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

Since systemPrompt remains unchanged throughout the method, consider caching its token estimate once rather than repeatedly recalculating it to improve maintainability.

Suggested change
var currentTokens = TokenHelper.EstimateTokens(systemPrompt, model);
var currentTokens = systemPromptTokens;

Copilot uses AI. Check for mistakes.
@PatrickRuddiman
Copy link
Owner Author

this may not be needed; ill do more testing

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.

2 participants