Date: 2025-02-02
Repository: /Users/michaelassraf/Documents/GitHub/CodeWiki
Added comprehensive logging for prompt injection and assembly throughout the CodeWiki documentation generation pipeline. Every prompt that goes to an LLM is now logged with full visibility into:
- Prompt template selection
- Component lengths and previews
- Variable substitutions
- Token estimates
- LLM API invocations
- Response details
All logs follow a consistent tree-based format for readability:
📝 Prompt Assembly Stage - TEMPLATE_NAME
├─ Template: TEMPLATE_NAME
├─ Module name: example_module
├─ Component 1: 1,234 chars
│ └─ Preview: First 100 characters...
├─ Component 2: 5,678 chars
├─ Total assembled prompt: 10,000 chars (~2,500 tokens)
└─ ✅ Prompt ready for LLM invocation
🤖 LLM API Invocation
├─ Stage: main/generation
├─ Model: claude-sonnet-4.5
├─ Base URL: https://api.anthropic.com
├─ Prompt length: 10,000 chars (~2,500 tokens)
│ └─ Preview: First 150 characters...
├─ Temperature: 0.0
├─ Max tokens: 16384 (field: max_tokens)
├─ Temperature supported: True
└─ 🚀 Sending request to LLM API...
✅ LLM Response Received
├─ Response length: 4,321 chars (~1,080 tokens)
└─ Preview: First 150 characters...
All prompt formatting functions now log detailed assembly information:
- Template: SYSTEM_PROMPT (complex modules)
- Logs:
- Module name
- Custom instructions length + preview
- Flamingo custom instructions section length + preview
- Flamingo guidelines section length + preview
- Base system prompt length
- Total assembled prompt length + token estimate
- Template: LEAF_SYSTEM_PROMPT (leaf modules)
- Logs: Same as
format_system_prompt
- Template: USER_PROMPT
- Logs:
- Module name
- Core component IDs count + list preview
- Module tree context length + preview
- Core component codes length
- Number of files included
- Base USER_PROMPT length
- Total assembled prompt length + token estimate
- Template: REPO_OVERVIEW_PROMPT
- Logs:
- Repository name
- Repository structure (JSON) length + preview
- Flamingo sections lengths
- Base prompt length
- Total assembled prompt length + token estimate
- Template: MODULE_OVERVIEW_PROMPT
- Logs: Same as
format_repo_overview_prompt
- Templates: CLUSTER_REPO_PROMPT or CLUSTER_MODULE_PROMPT
- Logs:
- Template selection (repo-level vs module-level)
- Module name (if applicable)
- Potential core components length + preview
- Module tree context length + preview (if module-level)
- Base prompt length
- Total assembled prompt length + token estimate
-
Logs Before Request:
- Stage identification (cluster/main/fallback)
- Model name
- Base URL
- Prompt length + token estimate + preview
- Temperature value
- Max tokens configuration (value + field name)
- Temperature support status
-
Logs After Response:
- Response length + token estimate
- Response preview
- New Logs:
- Agent execution section header
- Module name
- Agent type (Complex vs Leaf)
- User prompt length + token estimate
- Agent invocation indicator
- New Logs:
- Module clustering operation header
- Current module name
- Module path
- Leaf nodes count
- Components dictionary size
- Potential components token count
- Max token per module threshold
- Clustering decision (skip or proceed)
- LLM model used
- Response length + preview
- New Logs:
- Parent documentation generation header
- Module name
- Module path
- Repository structure JSON length
- Prompt type (MODULE_OVERVIEW vs REPO_OVERVIEW)
- LLM model used
- Generation status
-
SYSTEM_PROMPT - Complex modules with sub-modules
- Used for: Multi-file modules requiring sub-module generation
- Logged in:
format_system_prompt()
-
LEAF_SYSTEM_PROMPT - Simple leaf modules
- Used for: Single-file or simple modules without sub-modules
- Logged in:
format_leaf_system_prompt()
-
USER_PROMPT - Module documentation request
- Used for: Providing module context and code to agents
- Logged in:
format_user_prompt()
-
REPO_OVERVIEW_PROMPT - Repository-level overview
- Used for: Generating top-level repository documentation
- Logged in:
format_repo_overview_prompt()
-
MODULE_OVERVIEW_PROMPT - Parent module overview
- Used for: Generating aggregated parent module documentation
- Logged in:
format_module_overview_prompt()
-
CLUSTER_REPO_PROMPT - Repository-level clustering
- Used for: Grouping components into modules at repository level
- Logged in:
format_cluster_prompt()whenmodule_tree == {}
-
CLUSTER_MODULE_PROMPT - Sub-module clustering
- Used for: Grouping components within an existing module
- Logged in:
format_cluster_prompt()whenmodule_tree != {}
These prompts return JSON, not markdown, so they don't need markdown validation rules:
- CLUSTER_REPO_PROMPT - Returns JSON grouping
- CLUSTER_MODULE_PROMPT - Returns JSON grouping
- FILTER_FOLDERS_PROMPT - Returns JSON file list
All prompts that include Flamingo-specific content now log:
-
Flamingo Custom Instructions Section (
_CUSTOM_INSTRUCTIONS_SECTION)- Length in characters
- Preview of first 100 characters
- Source:
codewiki/src/be/flamingo_guidelines.py
-
Flamingo Guidelines Section (
_GUIDELINES_SECTION)- Length in characters
- Preview of first 100 characters
- Source:
codewiki/src/be/flamingo_guidelines.py
These sections are dynamically loaded from environment variables:
CODEWIKI_CUSTOM_INSTRUCTIONS_PATHCODEWIKI_GUIDELINES_PATH
📝 Prompt Assembly Stage - SYSTEM_PROMPT (complex modules)
├─ Template: SYSTEM_PROMPT (complex modules)
├─ Module name: openframe-api
├─ Custom instructions: None
├─ Flamingo custom instructions section: 2,456 chars
│ └─ Preview: You are working on the OpenFrame project...
├─ Flamingo guidelines section: 3,890 chars
│ └─ Preview: ## Markdown Validation Rules...
├─ Base system prompt length: 15,234 chars
├─ Total assembled prompt: 21,580 chars (~5,395 tokens)
└─ ✅ Prompt ready for LLM invocation
📝 Prompt Assembly Stage - USER_PROMPT
├─ Template: USER_PROMPT
├─ Module name: openframe-api
├─ Core component IDs: 12 components
│ └─ Components: UserController, AuthService, JwtTokenProvider, UserRepository, SecurityConfig ... and 7 more
├─ Module tree context: 456 chars
│ └─ Preview: openframe-api (current module)...
├─ Core component codes: 45,678 chars
│ └─ Files included: 8 files
├─ Base USER_PROMPT length: 789 chars
├─ Total assembled prompt: 46,923 chars (~11,730 tokens)
└─ ✅ Prompt ready for LLM invocation
📨 Agent Execution - User Prompt Ready
├─ Module: openframe-api
├─ Agent type: Complex (with sub-modules)
├─ User prompt length: 46,923 chars (~11,730 tokens)
└─ 🚀 Invoking agent with formatted prompts...
🤖 LLM API Invocation
├─ Stage: main/generation
├─ Model: claude-sonnet-4-5-20250514
├─ Base URL: https://api.anthropic.com
├─ Prompt length: 68,503 chars (~17,125 tokens)
│ └─ Preview: You are working on the OpenFrame project. This is a multi-platform system...
├─ Temperature: 0.0
├─ Max tokens: 128000 (field: max_tokens)
├─ Temperature supported: True
└─ 🚀 Sending request to LLM API...
✅ LLM Response Received
├─ Response length: 12,345 chars (~3,086 tokens)
└─ Preview: # OpenFrame API Service\n\nThe OpenFrame API Service is the central...
🗂️ Module Clustering Operation
├─ Current module: openframe-services
├─ Module path: openframe-services
├─ Leaf nodes to cluster: 45
└─ Components dictionary size: 234 components
├─ Potential components (with code): 48,000 tokens
├─ Max token per module: 36,369
└─ ✅ Proceeding with clustering - components exceed threshold
📝 Prompt Assembly Stage - CLUSTER_MODULE_PROMPT
├─ Template: CLUSTER_MODULE_PROMPT
├─ Module name: openframe-services
├─ Potential core components: 120,000 chars
│ └─ Preview: # openframe-gateway/src/main/java/Gateway.java...
├─ Module tree context: 2,345 chars
│ └─ Preview: openframe-services (current module)...
├─ Base CLUSTER_MODULE_PROMPT length: 1,234 chars
├─ Total assembled prompt: 123,579 chars (~30,894 tokens)
└─ ✅ Prompt ready for LLM invocation
🤖 Calling clustering LLM
├─ Model: gpt-4o-mini
└─ Prompt assembled via format_cluster_prompt()
🤖 LLM API Invocation
├─ Stage: cluster
├─ Model: gpt-4o-mini
├─ Base URL: https://api.openai.com/v1
├─ Prompt length: 123,579 chars (~30,894 tokens)
│ └─ Preview: Here is list of all potential core components...
├─ Temperature: 0.0
├─ Max tokens: 16384 (field: max_tokens)
├─ Temperature supported: True
└─ 🚀 Sending request to LLM API...
✅ LLM Response Received
├─ Response length: 2,890 chars (~722 tokens)
└─ Preview: <GROUPED_COMPONENTS>\n{\n "gateway": {...
- Full Visibility: Every prompt assembly and LLM call is logged with complete context
- Debugging: Easy to trace prompt construction issues and identify which template is being used
- Token Tracking: Approximate token counts help prevent context overflow
- Performance Monitoring: Track prompt and response sizes across the pipeline
- Injection Tracking: Clear visibility into custom instructions and guidelines injection
- Variable Substitution: All variable replacements are logged with previews
To see the logs in action:
# Enable verbose logging
export PYTHONUNBUFFERED=1
# Run documentation generation
python -m codewiki.cli.main generate \
--repo-path /path/to/repo \
--output-dir /path/to/output \
--main-model claude-sonnet-4-5-20250514 \
--cluster-model gpt-4o-mini \
--verbose
# Logs will show:
# - All prompt template selections
# - Component lengths and previews
# - Variable substitutions
# - LLM API calls with full context
# - Response details- INFO: All prompt assembly and LLM invocation logs
- WARNING: Skipped clustering operations, missing components
- ERROR: LLM API failures with full context
Potential improvements for the logging system:
- Add structured logging (JSON format) for machine parsing
- Track cumulative token usage per module
- Log timing information for performance profiling
- Add log filtering by prompt template type
- Create visualization dashboard for token usage patterns
- Export logs to external monitoring systems (Logfire, etc.)
Implementation Status: ✅ Complete
Location: CodeWiki repository at /Users/michaelassraf/Documents/GitHub/CodeWiki