File contents Prompt Optimization
Prompt Optimization
Compress Prompts
def compress_prompt(prompt: str, max_tokens: int) -> str:
"""Compress prompt to fit within token limit."""
current_tokens = estimate_tokens(prompt)
if current_tokens <= max_tokens:
return prompt
# Strategy 1: Remove redundant whitespace
compressed = " ".join(prompt.split())
if estimate_tokens(compressed) <= max_tokens:
return compressed
# Strategy 2: Truncate with ellipsis
target_chars = max_tokens * 4 - 20
compressed = prompt[:target_chars] + "... [truncated]"
return compressed
def optimize_system_prompt(prompt: str) -> str:
"""Optimize system prompt for token efficiency."""
# Remove unnecessary formatting
lines = prompt.strip().split('\n')
optimized_lines = []
for line in lines:
# Skip empty lines and excessive formatting
stripped = line.strip()
if stripped and not stripped.startswith('#' * 3):
optimized_lines.append(stripped)
return '\n'.join(optimized_lines)
Context Compression
def compress_context(
context: str,
max_tokens: int,
preserve_ratio: float = 0.5
) -> str:
"""Compress context while preserving key information."""
current_tokens = estimate_tokens(context)
if current_tokens <= max_tokens:
return context
# Split into chunks
paragraphs = context.split('\n\n')
if len(paragraphs) == 1:
# Single block - truncate from middle
char_limit = max_tokens * 4
half = char_limit // 2
return context[:half] + "\n[...content omitted...]\n" + context[-half:]
# Multiple paragraphs - keep first and last, summarize middle
preserve_count = max(2, int(len(paragraphs) * preserve_ratio))
keep_start = preserve_count // 2
keep_end = preserve_count - keep_start
kept = paragraphs[:keep_start] + ["[...additional context omitted...]"] + paragraphs[-keep_end:]
return '\n\n'.join(kept)
1 --- 2 name: 2183-prompt-optimization-ace7d990 3 description: Prompt Optimization 4 --- 5 # Prompt Optimization 6 7 ## Prompt Optimization 8 9 ### Compress Prompts 10 ```python 11 def compress_prompt(prompt: str, max_tokens: int) -> str: 12 """Compress prompt to fit within token limit.""" 13 current_tokens = estimate_tokens(prompt) 14 15 if current_tokens <= max_tokens: 16 return prompt 17 18 # Strategy 1: Remove redundant whitespace 19 compressed = " ".join(prompt.split()) 20 21 if estimate_tokens(compressed) <= max_tokens: 22 return compressed 23 24 # Strategy 2: Truncate with ellipsis 25 target_chars = max_tokens * 4 - 20 26 compressed = prompt[:target_chars] + "... [truncated]" 27 28 return compressed 29 30 def optimize_system_prompt(prompt: str) -> str: 31 """Optimize system prompt for token efficiency.""" 32 # Remove unnecessary formatting 33 lines = prompt.strip().split('\n') 34 optimized_lines = [] 35 36 for line in lines: 37 # Skip empty lines and excessive formatting 38 stripped = line.strip() 39 if stripped and not stripped.startswith('#' * 3): 40 optimized_lines.append(stripped) 41 42 return '\n'.join(optimized_lines) 43 ``` 44 45 ### Context Compression 46 ```python 47 def compress_context( 48 context: str, 49 max_tokens: int, 50 preserve_ratio: float = 0.5 51 ) -> str: 52 """Compress context while preserving key information.""" 53 current_tokens = estimate_tokens(context) 54 55 if current_tokens <= max_tokens: 56 return context 57 58 # Split into chunks 59 paragraphs = context.split('\n\n') 60 61 if len(paragraphs) == 1: 62 # Single block - truncate from middle 63 char_limit = max_tokens * 4 64 half = char_limit // 2 65 return context[:half] + "\n[...content omitted...]\n" + context[-half:] 66 67 # Multiple paragraphs - keep first and last, summarize middle 68 preserve_count = max(2, int(len(paragraphs) * preserve_ratio)) 69 keep_start = preserve_count // 2 70 keep_end = preserve_count - keep_start 71 72 kept = paragraphs[:keep_start] + ["[...additional context omitted...]"] + paragraphs[-keep_end:] 73 return '\n\n'.join(kept) 74 ```
tools-only/X-Skills/tree/main/data-analysis/2183-prompt-optimization_ace7d990 commit 25949dd456
Frequently asked questions How do I install the 2183 Prompt Optimization Ace7d990 skill? Run npx skillmds@latest add tools-only/2183-prompt-optimization-ace7d990 in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the 2183 Prompt Optimization Ace7d990 skill do? Prompt Optimization It is listed under AI & ML on SkillMD.
Is 2183 Prompt Optimization Ace7d990 safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with 2183 Prompt Optimization Ace7d990? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is 2183 Prompt Optimization Ace7d990 free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published 2183 Prompt Optimization Ace7d990? tools-only (@tools-only) published this skill. Their other Agent Skills are listed on their SkillMD profile.