Prompt Safe
by @alexunitario-sketch
Token-safe prompt assembly with memory orchestration. Use for any agent that needs to construct LLM prompts with memory retrieval. Guarantees no API failure due to token overflow. Implements two-phase context construction, memory safety valve, and hard limits on memory injection.
clawhub install prompt-assembleπ About This Skill
name: prompt-assemble description: Token-safe prompt assembly with memory orchestration. Use for any agent that needs to construct LLM prompts with memory retrieval. Guarantees no API failure due to token overflow. Implements two-phase context construction, memory safety valve, and hard limits on memory injection.
Prompt Assemble
Overview
A standardized, token-safe prompt assembly framework that guarantees API stability. Implements Two-Phase Context Construction and Memory Safety Valve to prevent token overflow while maximizing relevant context.
Design Goals:
When to Use
Use this skill when: 1. Building or modifying any agent that constructs prompts 2. Implementing memory retrieval systems 3. Adding new prompt-related logic to existing agents 4. Any scenario where token budget safety is required
Core Workflow
User Input
β
Need-Memory Decision
β
Minimal Context Build
β
Memory Retrieval (Optional)
β
Memory Summarization
β
Token Estimation
β
Safety Valve Decision
β
Final Prompt β LLM Call
Phase Details
Phase 0: Base Configuration
# Model Context Windows (2026-02-04)
- MiniMax-M2.1: 204,000 tokens (default)
- Claude 3.5 Sonnet: 200,000 tokens
- GPT-4o: 128,000 tokens
MAX_TOKENS = 204000 # Set to your model's context limit
SAFETY_MARGIN = 0.75 * MAX_TOKENS # Conservative: 75% threshold = 153,000 tokens
MEMORY_TOP_K = 3 # Max 3 memories
MEMORY_SUMMARY_MAX = 3 lines # Max 3 lines per memory
Design Philosophy:
Phase 1: Minimal Context
Phase 2: Memory Need Decision
def need_memory(user_input):
triggers = [
"previously",
"earlier we discussed",
"do you remember",
"as I mentioned before",
"continuing from",
"before we",
"last time",
"previously mentioned"
]
for trigger in triggers:
if trigger.lower() in user_input.lower():
return True
return False
Phase 3: Memory Retrieval (Optional)
memories = memory_search(query=user_input, top_k=MEMORY_TOP_K)
for mem in memories:
summarized_memories.append(summarize(mem, max_lines=MEMORY_SUMMARY_MAX))
Phase 4: Token Estimation
Calculate estimated tokens for base_context + summarized_memories.Phase 5: Safety Valve (Critical)
if estimated_tokens > SAFETY_MARGIN:
base_context.append("[System Notice] Relevant memory skipped due to token budget.")
return assemble(base_context)
Hard Rules:
Phase 6: Final Assembly
final_prompt = assemble(base_context + summarized_memories)
return final_prompt
Memory Data Standards
Allowed in Long-Term Memory
Forbidden in Long-Term Memory
Quick Start
Copy scripts/prompt_assemble.py to your agent and use:
from prompt_assemble import build_promptIn your agent's prompt construction:
final_prompt = build_prompt(user_input, memory_search_fn, get_recent_dialog_fn)
Resources
scripts/
prompt_assemble.py - Complete implementation with all phases (PromptAssembler class)references/
memory_standards.md - Detailed memory content guidelinestoken_estimation.md - Token counting strategiesβ‘ When to Use
π‘ Examples
Copy scripts/prompt_assemble.py to your agent and use:
from prompt_assemble import build_promptIn your agent's prompt construction:
final_prompt = build_prompt(user_input, memory_search_fn, get_recent_dialog_fn)