๐ฌ LLM Context ManagementJune 3, 2026โ
Tests passing
Dynamic Context Packer
This Python CLI tool dynamically packages mixed data types (e.g., text, metadata, tables) into a single context window for large language models. It uses adaptive formatting based on token limits and user-defined rules to optimize how information is presented. This is particularly useful for tasks requiring complex context, like generating multi-modal reports or providing detailed answers.
What It Does
- Handles mixed data types (text, metadata, etc.)
- Adaptive formatting to fit token constraints
- User-defined packing rules and priorities
Installation
- Python 3.8+
- pandas==1.5.3
- tiktoken==0.3.0
Usage
python dynamic_context_packer.py --input data.json --max_tokens 4096 --output packed_context.txtExample
python dynamic_context_packer.py --input data1.json data2.json --max_tokens 2048 --output packed_context.jsonSource Code
import json
import argparse
import pandas as pd
import tiktoken
def load_data(input_files):
"""Load data from JSON files."""
data = []
for file in input_files:
try:
with open(file, 'r') as f:
data.append(json.load(f))
except FileNotFoundError:
raise FileNotFoundError(f"File not found: {file}")
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON format in file: {file}")
return data
def calculate_tokens(text, encoding_name='cl100k_base'):
"""Calculate the number of tokens in a given text using tiktoken."""
encoding = tiktoken.get_encoding(encoding_name)
return len(encoding.encode(text))
def pack_context(data, max_tokens, rules):
"""Pack mixed data types into a single context window."""
packed_context = ""
for item in data:
if isinstance(item, dict):
for key, value in item.items():
entry = f"{key}: {value}\n"
if calculate_tokens(packed_context + entry) <= max_tokens:
packed_context += entry
else:
break
elif isinstance(item, list):
for sub_item in item:
entry = f"- {sub_item}\n"
if calculate_tokens(packed_context + entry) <= max_tokens:
packed_context += entry
else:
break
else:
entry = f"{item}\n"
if calculate_tokens(packed_context + entry) <= max_tokens:
packed_context += entry
else:
break
return packed_context
def save_output(output, output_file):
"""Save the packed context to a file."""
try:
with open(output_file, 'w') as f:
f.write(output)
except Exception as e:
raise IOError(f"Failed to save output to {output_file}: {e}")
def main():
parser = argparse.ArgumentParser(description="Dynamic Context Packer")
parser.add_argument('--input', nargs='+', required=True, help="Input JSON file(s)")
parser.add_argument('--max_tokens', type=int, required=True, help="Maximum token limit")
parser.add_argument('--output', required=True, help="Output file")
parser.add_argument('--rules', type=str, default='', help="Packing rules (optional)")
args = parser.parse_args()
try:
data = load_data(args.input)
packed_context = pack_context(data, args.max_tokens, args.rules)
save_output(packed_context, args.output)
print(f"Context packed successfully into {args.output}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- dynamic_context_packer
- Category
- LLM Context Management
- Generated
- June 3, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-06-03/dynamic_context_packer cd generated_tools/2026-06-03/dynamic_context_packer pip install -r requirements.txt 2>/dev/null || true python dynamic_context_packer.py