Token Optimization for LLM Extraction Prompts via Pre-Extracting
Summary
Optimize the information extraction process that leverages Large Language Models (LLMs) by eliminating token inefficiency caused by including verbose, raw database data in LLM prompts. Only concise, relevant, and parsed information should be provided to the LLM, reducing inference costs, latency, and the risk of exceeding context window limits.
Problem Statement
The current information extraction pipeline includes raw, verbose "Extracted data from database" directly in the LLM prompt, alongside "User input" and "AI Message." This practice leads to excessively long prompts, resulting in:
- Higher inference costs
- Increased processing latency
- Potential context window overflows for the LLM
The objective is to prevent raw database data from entering the LLM prompt and ensure only relevant, concise, and parsed information is provided for extraction.
Technical Requirements
- Implement a preprocessing/summarization utility to process database data before it is included in any LLM prompt.
- The utility must:
- Remove irrelevant or verbose fields from database records.
- Summarize or extract only the essential information needed for the extraction task.
- Be easily configurable to adjust what is considered "relevant" or "concise."
- Integrate this utility at all points where database data is added to LLM prompts, especially in:
src/core/brain/tools/definitions/memory/extract_and_operate_memory.tssrc/core/brain/tools/definitions/memory/memory_operation.ts
- Refactor prompt construction logic to:
- Enforce a maximum token budget for each prompt section (user input, AI message, database data).
- Truncate or summarize content as needed to stay within budget.
- Ensure backward compatibility and do not break existing extraction or memory operations.
Implementation Guidelines
-
Utility Creation
- Create a new utility function (e.g.,
summarizeDatabaseDataForPrompt) in a shared location such assrc/core/brain/tools/utils/or an appropriate utilities file. - The function should accept raw database data and return a concise, relevant string or object for prompt inclusion.
- Allow configuration of summarization rules (e.g., via a config file or environment variable).
- Create a new utility function (e.g.,
-
Integration
- In
extract_and_operate_memory.ts, apply the utility to any database data before it is included in the LLM prompt. - In
memory_operation.ts, ensure that functions likeformatSimilarMemoriesForLLMuse the utility to summarize or truncate memory content. - Apply similar logic to any other prompt construction code that may include database data.
- In
-
Prompt Refactoring
- Enforce a maximum token or character budget for each section of the prompt.
- Truncate or further summarize content as needed to stay within budget.
-
Testing
- Write unit tests for the summarization utility, covering various database data formats and edge cases.
- Write integration tests to ensure that LLM prompts are concise and relevant, and that extraction quality is maintained or improved.
- Add performance tests to measure reduction in token usage and latency.
-
Documentation
- Document the new utility, its configuration options, and its integration points.
- Update any relevant developer documentation or code comments.
Definition of Done
- A shared summarization/preprocessing utility is implemented and tested.
- All relevant prompt construction code uses the utility to process database data.
- Prompts sent to the LLM are demonstrably shorter, more relevant, and within context window limits.
- Unit, integration, and performance tests pass.
- Documentation is updated to reflect the new process.
- No regression in extraction or memory operation quality.
Code Reference
Key files to create/modify:
src/core/brain/tools/utils/summarizeDatabaseDataForPrompt.ts(new utility)src/core/brain/tools/definitions/memory/extract_and_operate_memory.ts(apply utility before prompt construction)src/core/brain/tools/definitions/memory/memory_operation.ts(apply utility in formatting functions)- (Optional) Configuration file or section for summarization rules
Example references for similar logic:
- Prompt construction and memory formatting in
extract_and_operate_memory.tsandmemory_operation.ts - Utility patterns in
src/core/brain/tools/utils/
Additional Notes
- If the codebase supports multiple data types or sources, ensure the utility is extensible for future needs.
- Consider logging or metrics to monitor prompt length and summarization effectiveness after deployment.
- If a migration is needed for legacy data, scope and plan this as a follow-up task.
If further clarification or a more detailed breakdown is needed, please request a deeper code reference analysis.