- Claude 4
- OpenAI Codex
- Google Gemini CLI
- AI Development Tools
- Implementation Guide
- Development Environment Setup
- implementation-guide categories:
- AI Development & Automation
- Tools & Development Efficiency author: Claude Code status: practical experience_level: intermediate
Claude 4, OpenAI Codex & Gemini CLI Implementation Guide: Practical Integration Steps for Developers 2025¶
Introduction¶
This guide provides detailed implementation methods and practical examples for the latest AI development tools introduced in Must-See for AI Developers: Claude 4 Launch, OpenAI Codex & Terminal Revolution - July 22, 2025 Breaking News (archived). A concrete guide for developers moving from theory to practice.
Key Points¶
Environment Setup Automation
Build an optimal development environment integrating three AI tools in 30 minutes
Code Generation Optimization
Efficient code generation workflow leveraging each tool's strengths
Secure Integration
API configuration and data protection procedures safe for enterprise environments
Performance Measurement
Quantitative productivity evaluation and bottleneck identification methods
Claude 4 Implementation: Building Long-Term Task Support Environment¶
Environment Setup¶
# Build development environment for Claude 4
mkdir claude4-workspace && cd claude4-workspace
# Install required dependencies
pip install anthropic python-dotenv pyyaml
# Prepare configuration file
cat > claude4_config.yaml << 'EOF'
# Claude 4 configuration file
claude_config:
model: "claude-4-opus-20250514"
max_tokens: 8192
temperature: 0.1
project_settings:
context_window: 200000
long_task_mode: true
multi_file_analysis: true
security:
api_key_env: "CLAUDE_API_KEY"
log_level: "INFO"
sensitive_data_filter: true
EOF
Claude 4 Client Implementation¶
# claude4_client.py
import os
import yaml
from anthropic import Anthropic
from typing import List, Dict, Any
import logging
class Claude4Client:
def __init__(self, config_path: str = "claude4_config.yaml"):
"""Initialize Claude 4 client"""
with open(config_path, 'r') as f:
self.config = yaml.safe_load(f)
self.client = Anthropic(
api_key=os.getenv(self.config['security']['api_key_env'])
)
# Logging setup
logging.basicConfig(
level=getattr(logging, self.config['security']['log_level'])
)
self.logger = logging.getLogger(__name__)
def analyze_codebase(self, file_paths: List[str]) -> Dict[str, Any]:
"""Comprehensive analysis of multiple files"""
combined_content = ""
file_map = {}
for path in file_paths:
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
file_map[path] = len(combined_content)
combined_content += f"\n\n--- {path} ---\n{content}"
except Exception as e:
self.logger.error(f"File read error: {path} - {e}")
message = self.client.messages.create(
model=self.config['claude_config']['model'],
max_tokens=self.config['claude_config']['max_tokens'],
temperature=self.config['claude_config']['temperature'],
messages=[
{
"role": "user",
"content": f"""
Analyze the following codebase and provide:
1. Architecture overview
2. Potential issues
3. Improvement suggestions
4. Dependency analysis
Codebase:
{combined_content}
"""
}
]
)
return {
"analysis": message.content[0].text,
"file_count": len(file_paths),
"total_tokens": len(combined_content.split())
}
def long_term_refactoring(self, project_path: str, task_description: str) -> str:
"""Execute long-term refactoring task"""
# Analyze project structure
import os
import glob
python_files = glob.glob(f"{project_path}/**/*.py", recursive=True)
js_files = glob.glob(f"{project_path}/**/*.js", recursive=True)
analysis = self.analyze_codebase(python_files + js_files)
refactoring_prompt = f"""
Long-term refactoring task: {task_description}
Current analysis results:
{analysis['analysis']}
Create a concrete refactoring plan in the following format:
1. Phased execution steps (each phase within 1-2 hours)
2. Specific code changes for each phase
3. Testing strategy
4. Risk assessment and countermeasures
"""
message = self.client.messages.create(
model=self.config['claude_config']['model'],
max_tokens=self.config['claude_config']['max_tokens'],
messages=[{"role": "user", "content": refactoring_prompt}]
)
return message.content[0].text
# Usage example
if __name__ == "__main__":
client = Claude4Client()
# Multiple file analysis example
files_to_analyze = ["src/main.py", "src/utils.py", "tests/test_main.py"]
result = client.analyze_codebase(files_to_analyze)
print("Analysis result:", result['analysis'])
# Generate long-term refactoring plan
plan = client.long_term_refactoring(
"./my_project",
"Split monolith into microservices"
)
print("Refactoring plan:", plan)
Claude 4 Utilization Tips
To maximize Claude 4's long-term task execution capability, structure tasks clearly and save intermediate results regularly
OpenAI Codex Implementation: ChatGPT Integrated Development Environment¶
Codex Agent Configuration¶
# codex_agent.py
import openai
import json
import subprocess
import os
from typing import Dict, List, Any
class CodexAgent:
def __init__(self, api_key: str):
"""Initialize OpenAI Codex Agent"""
openai.api_key = api_key
self.conversation_history = []
def create_feature(self, feature_description: str, project_context: str) -> Dict[str, Any]:
"""Implement complete software feature"""
system_prompt = f"""
You are an experienced software developer. Implement a new feature with the following project context.
Project context:
{project_context}
Implementation requirements:
1. Detailed feature specification
2. Required files and directory structure
3. Implementation code (complete version)
4. Test code
5. Documentation
"""
response = openai.ChatCompletion.create(
model="gpt-4-code-interpreter", # Codex-specific model
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Feature to implement: {feature_description}"}
],
temperature=0.1,
max_tokens=4000
)
result = response.choices[0].message.content
# Structure the result
return self._parse_implementation_result(result)
def fix_bugs(self, error_log: str, source_code: str) -> Dict[str, str]:
"""Automatic bug fixing"""
bug_fix_prompt = f"""
Analyze the following error log and source code, identify bugs, and provide fixed code.
Error log:
{error_log}
Source code:
{source_code}
Answer in the following format:
1. Bug cause
2. Fixed code
3. Fix explanation
4. Additional test suggestions
"""
response = openai.ChatCompletion.create(
model="gpt-4-code-interpreter",
messages=[{"role": "user", "content": bug_fix_prompt}],
temperature=0.1
)
return self._parse_bug_fix_result(response.choices[0].message.content)
def run_tests(self, test_command: str = "pytest") -> Dict[str, Any]:
"""Comprehensive test automation"""
try:
# Execute tests
result = subprocess.run(
test_command.split(),
capture_output=True,
text=True,
timeout=300
)
test_output = {
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode,
"success": result.returncode == 0
}
# Analyze test results with Codex
if not test_output["success"]:
analysis = self._analyze_test_failures(test_output)
test_output["analysis"] = analysis
test_output["suggested_fixes"] = self._suggest_test_fixes(analysis)
return test_output
except subprocess.TimeoutExpired:
return {"error": "Test execution timed out"}
except Exception as e:
return {"error": f"Test execution error: {str(e)}"}
def _parse_implementation_result(self, result: str) -> Dict[str, Any]:
"""Parse implementation result"""
# Structure implementation result (in actual implementation use regex or NLP)
return {
"specification": "Feature specification...",
"file_structure": ["file1.py", "file2.py"],
"implementation": {"file1.py": "code content..."},
"tests": {"test_file1.py": "test code..."},
"documentation": "README content..."
}
def _parse_bug_fix_result(self, result: str) -> Dict[str, str]:
"""Parse bug fix result"""
return {
"bug_cause": "Bug cause...",
"fixed_code": "Fixed code...",
"explanation": "Fix explanation...",
"additional_tests": "Additional tests..."
}
def _analyze_test_failures(self, test_output: Dict[str, Any]) -> str:
"""Analyze test failures"""
analysis_prompt = f"""
Analyze the following test execution results and identify the cause of failures:
Standard output:
{test_output['stdout']}
Error output:
{test_output['stderr']}
"""
response = openai.ChatCompletion.create(
model="gpt-4-code-interpreter",
messages=[{"role": "user", "content": analysis_prompt}],
temperature=0.1
)
return response.choices[0].message.content
def _suggest_test_fixes(self, analysis: str) -> List[str]:
"""Suggest test fixes"""
# Generate fix suggestions based on analysis
return ["Fix suggestion 1", "Fix suggestion 2", "Fix suggestion 3"]
# Usage example and workflow integration
def create_development_workflow():
"""Create development workflow"""
agent = CodexAgent(os.getenv("OPENAI_API_KEY"))
# 1. Implement new feature
feature_result = agent.create_feature(
"User authentication system",
"Flask web application, using PostgreSQL database"
)
# 2. Save implemented code
for filename, content in feature_result["implementation"].items():
with open(filename, 'w') as f:
f.write(content)
# 3. Run tests
test_result = agent.run_tests()
# 4. Fix bugs if any
if not test_result["success"]:
for filename in feature_result["implementation"].keys():
with open(filename, 'r') as f:
source_code = f.read()
bug_fix = agent.fix_bugs(test_result["stderr"], source_code)
# Save fixed version
with open(filename, 'w') as f:
f.write(bug_fix["fixed_code"])
return feature_result, test_result
if __name__ == "__main__":
create_development_workflow()
GitHub Actions Integration¶
# .github/workflows/codex-integration.yml
name: OpenAI Codex Integration
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
jobs:
codex-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install openai pytest black flake8
- name: Run Codex Code Review
run: |
python scripts/codex_review.py --changed-files ${{ github.event.pull_request.changed_files }}
- name: Auto-fix Code Issues
run: |
python scripts/codex_autofix.py
- name: Run Tests with Codex
run: |
python scripts/codex_test_runner.py
- name: Comment PR with Analysis
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const analysis = fs.readFileSync('codex_analysis.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: analysis
});
Notes When Using Codex
OpenAI Codex is a research preview, so conduct thorough testing and validation before production use
Google Gemini CLI Implementation: Terminal Integration¶
Installation and Initial Configuration¶
# Install Gemini CLI
curl -fsSL https://cli.gemini.google.com/install.sh | bash
# Set environment variables
export GEMINI_API_KEY="your_api_key_here"
export GEMINI_PROJECT_ID="your_project_id"
# Create configuration file
mkdir -p ~/.config/gemini
cat > ~/.config/gemini/config.toml << 'EOF'
[default]
model = "gemini-2.0-flash-exp"
temperature = 0.1
max_tokens = 8192
[coding]
model = "gemini-2.0-flash-thinking-exp-1219"
temperature = 0.0
context_window = 32000
[security]
log_requests = false
sanitize_output = true
respect_gitignore = true
EOF
Creating Custom Commands¶
# ~/.local/bin/ai-dev (make executable: chmod +x ~/.local/bin/ai-dev)
#!/bin/bash
# AI development assistance script
set -e
COMMAND=$1
shift
case $COMMAND in
"review")
echo "🔍 Running code review..."
gemini --profile coding "Review the following code and point out improvements and potential issues. Analyze especially from security, performance, and maintainability perspectives." --files "$@"
;;
"optimize")
echo "⚡ Generating performance optimization suggestions..."
gemini --profile coding "Analyze the performance of the following code and present optimization suggestions with concrete code examples." --files "$@"
;;
"test")
echo "🧪 Generating test cases..."
gemini --profile coding "Generate comprehensive test cases for the following code. Include edge cases, error handling, and performance tests." --files "$@"
;;
"debug")
if [ -f "error.log" ]; then
echo "🐛 Analyzing debug..."
gemini --profile coding "Analyze the following error log and source code to suggest the cause and solution." --files error.log "$@"
else
echo "❌ error.log file not found"
exit 1
fi
;;
"refactor")
echo "🔧 Generating refactoring suggestions..."
gemini --profile coding "Refactor the following code and present concrete improvements to enhance readability, maintainability, and extensibility." --files "$@"
;;
"explain")
echo "📖 Generating code explanation..."
gemini --profile coding "Explain the behavior of the following code in detail. Describe from algorithm, data structure, and design pattern perspectives." --files "$@"
;;
*)
echo "AI Development Assistant"
echo ""
echo "Usage: ai-dev <command> [files...]"
echo ""
echo "Commands:"
echo " review - Run code review"
echo " optimize - Performance optimization suggestions"
echo " test - Generate test cases"
echo " debug - Error analysis (requires error.log)"
echo " refactor - Refactoring suggestions"
echo " explain - Code explanation"
echo ""
echo "Examples:"
echo " ai-dev review src/main.py"
echo " ai-dev test src/utils.py"
echo " ai-dev debug src/problematic.py"
;;
esac
Advanced Workflow Integration¶
# gemini_workflow.py
import subprocess
import json
import os
from typing import Dict, List, Any
class GeminiWorkflow:
def __init__(self, config_path: str = "~/.config/gemini/config.toml"):
self.config_path = config_path
def batch_code_analysis(self, project_path: str) -> Dict[str, Any]:
"""Batch analysis of entire project"""
# Collect Python files
python_files = self._collect_files(project_path, "*.py")
# Collect JavaScript/TypeScript files
js_files = self._collect_files(project_path, "*.js") + \
self._collect_files(project_path, "*.ts")
results = {
"python_analysis": self._analyze_files(python_files, "python"),
"javascript_analysis": self._analyze_files(js_files, "javascript"),
"architecture_analysis": self._analyze_architecture(project_path),
"security_analysis": self._security_scan(project_path)
}
return results
def _collect_files(self, project_path: str, pattern: str) -> List[str]:
"""Collect files based on pattern"""
result = subprocess.run(
f"find {project_path} -name '{pattern}' -type f",
shell=True,
capture_output=True,
text=True
)
return result.stdout.strip().split('\n') if result.stdout.strip() else []
def _analyze_files(self, files: List[str], language: str) -> Dict[str, Any]:
"""Language-specific analysis"""
if not files:
return {"status": "no_files", "language": language}
# Limit to maximum 5 files (API rate limit countermeasure)
files_to_analyze = files[:5]
analysis_results = {}
for file_path in files_to_analyze:
try:
result = subprocess.run(
f"gemini --profile coding 'Evaluate the quality of this code and suggest improvements' --files {file_path}",
shell=True,
capture_output=True,
text=True,
timeout=60
)
analysis_results[file_path] = {
"status": "success",
"analysis": result.stdout,
"issues": self._extract_issues(result.stdout)
}
except subprocess.TimeoutExpired:
analysis_results[file_path] = {
"status": "timeout",
"error": "Analysis timed out"
}
except Exception as e:
analysis_results[file_path] = {
"status": "error",
"error": str(e)
}
return {
"language": language,
"files_analyzed": len(analysis_results),
"results": analysis_results
}
def _analyze_architecture(self, project_path: str) -> Dict[str, Any]:
"""Architecture analysis"""
try:
# Get project structure
structure_result = subprocess.run(
f"tree {project_path} -I '__pycache__|node_modules|.git'",
shell=True,
capture_output=True,
text=True
)
# Architecture analysis by Gemini
gemini_result = subprocess.run(
f"echo '{structure_result.stdout}' | gemini --profile coding 'Analyze this project structure and describe architectural features, design patterns, and improvement suggestions'",
shell=True,
capture_output=True,
text=True
)
return {
"status": "success",
"project_structure": structure_result.stdout,
"architecture_analysis": gemini_result.stdout
}
except Exception as e:
return {
"status": "error",
"error": str(e)
}
def _security_scan(self, project_path: str) -> Dict[str, Any]:
"""Security scan"""
security_patterns = [
"password.*=",
"api[_-]?key.*=",
"secret.*=",
"token.*=",
"eval\\(",
"exec\\(",
"subprocess\\.call"
]
findings = []
for pattern in security_patterns:
try:
result = subprocess.run(
f"grep -r -n '{pattern}' {project_path} --include='*.py' --include='*.js' --include='*.ts'",
shell=True,
capture_output=True,
text=True
)
if result.stdout:
findings.append({
"pattern": pattern,
"matches": result.stdout.strip().split('\n')
})
except Exception as e:
continue
return {
"status": "completed",
"findings_count": len(findings),
"findings": findings,
"risk_level": "high" if len(findings) > 5 else "medium" if len(findings) > 0 else "low"
}
def _extract_issues(self, analysis_text: str) -> List[str]:
"""Extract issues from analysis results"""
# Simple implementation example (actual needs more sophisticated NLP processing)
issues = []
lines = analysis_text.split('\n')
for line in lines:
if any(keyword in line.lower() for keyword in ['issue', 'bug', 'error', 'improve', 'problem']):
issues.append(line.strip())
return issues
# Usage example
def main():
workflow = GeminiWorkflow()
# Execute project analysis
project_path = "./my_project"
analysis_results = workflow.batch_code_analysis(project_path)
# Save results
with open("analysis_report.json", "w", encoding="utf-8") as f:
json.dump(analysis_results, f, indent=2, ensure_ascii=False)
# Generate summary report
print("🔍 Project analysis completed")
print(f"Python files analyzed: {analysis_results['python_analysis']['files_analyzed']}")
print(f"JavaScript files analyzed: {analysis_results['javascript_analysis']['files_analyzed']}")
print(f"Security risk level: {analysis_results['security_analysis']['risk_level']}")
if __name__ == "__main__":
main()
Gemini CLI Utilization Tips
For efficient terminal work, leverage aliases and custom scripts to shorten frequently used commands
Building Integrated Workflow¶
Three-Tool Integration Script¶
# integrated_ai_workflow.py
import asyncio
import json
from datetime import datetime
from claude4_client import Claude4Client
from codex_agent import CodexAgent
from gemini_workflow import GeminiWorkflow
class IntegratedAIWorkflow:
def __init__(self):
self.claude = Claude4Client()
self.codex = CodexAgent(os.getenv("OPENAI_API_KEY"))
self.gemini = GeminiWorkflow()
async def comprehensive_project_analysis(self, project_path: str) -> Dict[str, Any]:
"""Comprehensive project analysis using three AI tools"""
print("🚀 Starting comprehensive project analysis...")
# Execute analysis with each tool in parallel
tasks = [
self._claude_analysis(project_path),
self._codex_analysis(project_path),
self._gemini_analysis(project_path)
]
claude_result, codex_result, gemini_result = await asyncio.gather(*tasks)
# Integrate results
integrated_result = self._integrate_analyses(
claude_result, codex_result, gemini_result
)
# Generate final report
final_report = self._generate_final_report(integrated_result)
return {
"timestamp": datetime.now().isoformat(),
"project_path": project_path,
"individual_analyses": {
"claude": claude_result,
"codex": codex_result,
"gemini": gemini_result
},
"integrated_analysis": integrated_result,
"final_report": final_report
}
async def _claude_analysis(self, project_path: str) -> Dict[str, Any]:
"""Analysis from long-term perspective with Claude 4"""
print("🧠 Analyzing with Claude 4...")
# Collect project files
import glob
python_files = glob.glob(f"{project_path}/**/*.py", recursive=True)[:10]
if python_files:
analysis = self.claude.analyze_codebase(python_files)
# Also generate long-term refactoring plan
refactoring_plan = self.claude.long_term_refactoring(
project_path,
"Improve maintainability and scalability of entire codebase"
)
return {
"tool": "Claude 4",
"strength": "Long-term perspective and architecture analysis",
"codebase_analysis": analysis,
"refactoring_plan": refactoring_plan,
"status": "success"
}
else:
return {
"tool": "Claude 4",
"status": "no_python_files",
"message": "No Python files found for analysis"
}
async def _codex_analysis(self, project_path: str) -> Dict[str, Any]:
"""Implementation-focused analysis with OpenAI Codex"""
print("🤖 Analyzing with OpenAI Codex...")
try:
# Run tests
test_result = self.codex.run_tests("python -m pytest tests/")
# Bug fix suggestions (if tests failed)
bug_fixes = []
if not test_result.get("success", False):
# Simplified implementation
bug_fixes = ["Generate fix suggestions for test failures"]
return {
"tool": "OpenAI Codex",
"strength": "Implementation and test automation",
"test_results": test_result,
"bug_fixes": bug_fixes,
"status": "success"
}
except Exception as e:
return {
"tool": "OpenAI Codex",
"status": "error",
"error": str(e)
}
async def _gemini_analysis(self, project_path: str) -> Dict[str, Any]:
"""Practical analysis with Google Gemini CLI"""
print("💎 Analyzing with Google Gemini CLI...")
try:
analysis = self.gemini.batch_code_analysis(project_path)
return {
"tool": "Google Gemini CLI",
"strength": "Practical issue detection and security",
"analysis": analysis,
"status": "success"
}
except Exception as e:
return {
"tool": "Google Gemini CLI",
"status": "error",
"error": str(e)
}
def _integrate_analyses(self, claude_result: Dict, codex_result: Dict, gemini_result: Dict) -> Dict[str, Any]:
"""Integrate three analysis results"""
# Extract common issues
common_issues = self._find_common_issues(claude_result, codex_result, gemini_result)
# Generate prioritized task list
prioritized_tasks = self._generate_prioritized_tasks(claude_result, codex_result, gemini_result)
# Role assignment leveraging each tool's strengths
tool_recommendations = {
"long_term_planning": "Adopt Claude 4's long-term refactoring plan",
"immediate_fixes": "Prioritize implementing OpenAI Codex bug fix suggestions",
"security_hardening": "Implement countermeasures based on Gemini CLI security analysis"
}
return {
"common_issues": common_issues,
"prioritized_tasks": prioritized_tasks,
"tool_recommendations": tool_recommendations,
"integration_timestamp": datetime.now().isoformat()
}
def _find_common_issues(self, *results) -> List[str]:
"""Extract common issues from multiple analysis results"""
# Simplified implementation
return [
"Need for performance optimization",
"Improvement of error handling",
"Lack of documentation"
]
def _generate_prioritized_tasks(self, *results) -> List[Dict[str, Any]]:
"""Generate prioritized task list"""
return [
{
"priority": "high",
"task": "Fix security vulnerabilities",
"estimated_time": "2-4 hours",
"assigned_tool": "Gemini CLI"
},
{
"priority": "medium",
"task": "Fix test failures",
"estimated_time": "1-2 hours",
"assigned_tool": "OpenAI Codex"
},
{
"priority": "low",
"task": "Long-term architecture improvement",
"estimated_time": "1-2 weeks",
"assigned_tool": "Claude 4"
}
]
def _generate_final_report(self, integrated_analysis: Dict[str, Any]) -> str:
"""Generate final report"""
report = f"""
# Comprehensive AI Analysis Report
## Analysis Summary
- Common issues: {len(integrated_analysis['common_issues'])}
- Priority tasks: {len(integrated_analysis['prioritized_tasks'])}
- Generated: {integrated_analysis['integration_timestamp']}
## Key Findings
{chr(10).join([f"- {issue}" for issue in integrated_analysis['common_issues']])}
## Recommended Actions
{chr(10).join([f"- {task['task']} (Priority: {task['priority']}, Est. time: {task['estimated_time']})" for task in integrated_analysis['prioritized_tasks']])}
## Recommended Use of Each Tool
- **Long-term planning**: {integrated_analysis['tool_recommendations']['long_term_planning']}
- **Immediate fixes**: {integrated_analysis['tool_recommendations']['immediate_fixes']}
- **Security**: {integrated_analysis['tool_recommendations']['security_hardening']}
"""
return report.strip()
# Main execution function
async def main():
workflow = IntegratedAIWorkflow()
project_path = "./my_project" # Project to analyze
try:
result = await workflow.comprehensive_project_analysis(project_path)
# Save results
with open("comprehensive_analysis_report.json", "w", encoding="utf-8") as f:
json.dump(result, f, indent=2, ensure_ascii=False)
print("📋 Final Report:")
print(result["final_report"])
print(f"\n✅ Detailed analysis results saved to comprehensive_analysis_report.json")
except Exception as e:
print(f"❌ Error occurred during analysis: {e}")
if __name__ == "__main__":
asyncio.run(main())
Performance Measurement and Optimization¶
Productivity Measurement Tool¶
# productivity_metrics.py
import time
import json
import subprocess
from datetime import datetime, timedelta
from typing import Dict, List, Any
class ProductivityMetrics:
def __init__(self):
self.metrics_file = "ai_productivity_metrics.json"
self.load_existing_metrics()
def load_existing_metrics(self):
"""Load existing measurement results"""
try:
with open(self.metrics_file, 'r') as f:
self.metrics = json.load(f)
except FileNotFoundError:
self.metrics = {
"sessions": [],
"summary": {
"total_sessions": 0,
"average_session_time": 0,
"total_lines_generated": 0,
"total_bugs_fixed": 0
}
}
def start_session(self, task_description: str, ai_tools_used: List[str]) -> str:
"""Start development session"""
session_id = f"session_{int(time.time())}"
session_data = {
"session_id": session_id,
"task_description": task_description,
"ai_tools_used": ai_tools_used,
"start_time": datetime.now().isoformat(),
"end_time": None,
"duration_minutes": 0,
"lines_generated": 0,
"files_modified": [],
"bugs_fixed": 0,
"tests_written": 0,
"status": "in_progress"
}
self.current_session = session_data
print(f"📊 Session started: {session_id}")
print(f"Task: {task_description}")
print(f"Tools used: {', '.join(ai_tools_used)}")
return session_id
def end_session(self, session_id: str, final_status: str = "completed") -> Dict[str, Any]:
"""End development session"""
if not hasattr(self, 'current_session'):
raise ValueError("No active session found")
# Record session end time
end_time = datetime.now()
start_time = datetime.fromisoformat(self.current_session["start_time"])
duration = end_time - start_time
self.current_session.update({
"end_time": end_time.isoformat(),
"duration_minutes": int(duration.total_seconds() / 60),
"status": final_status
})
# Analyze code changes
self._analyze_code_changes()
# Save session data
self.metrics["sessions"].append(self.current_session)
self._update_summary()
self._save_metrics()
# Display results
self._display_session_results()
return self.current_session
def _analyze_code_changes(self):
"""Analyze code changes"""
try:
# Analyze changes using Git
git_diff = subprocess.run(
["git", "diff", "--numstat", "HEAD"],
capture_output=True,
text=True
)
if git_diff.stdout:
lines = git_diff.stdout.strip().split('\n')
total_additions = 0
files_modified = []
for line in lines:
parts = line.split('\t')
if len(parts) >= 3:
additions = int(parts[0]) if parts[0] != '-' else 0
filename = parts[2]
total_additions += additions
files_modified.append(filename)
self.current_session["lines_generated"] = total_additions
self.current_session["files_modified"] = files_modified
# Detect test files
test_files = [f for f in self.current_session["files_modified"]
if "test" in f.lower() or f.endswith("_test.py")]
self.current_session["tests_written"] = len(test_files)
except Exception as e:
print(f"⚠️ Error during code change analysis: {e}")
def _update_summary(self):
"""Update summary information"""
sessions = self.metrics["sessions"]
if sessions:
total_sessions = len(sessions)
total_time = sum(s["duration_minutes"] for s in sessions)
total_lines = sum(s["lines_generated"] for s in sessions)
total_bugs = sum(s["bugs_fixed"] for s in sessions)
self.metrics["summary"].update({
"total_sessions": total_sessions,
"average_session_time": total_time / total_sessions if total_sessions > 0 else 0,
"total_lines_generated": total_lines,
"total_bugs_fixed": total_bugs,
"last_updated": datetime.now().isoformat()
})
def _save_metrics(self):
"""Save metrics file"""
with open(self.metrics_file, 'w') as f:
json.dump(self.metrics, f, indent=2, ensure_ascii=False)
def _display_session_results(self):
"""Display session results"""
session = self.current_session
print("\n📈 Session Results:")
print(f" Time: {session['duration_minutes']} minutes")
print(f" Lines generated: {session['lines_generated']} lines")
print(f" Files modified: {len(session['files_modified'])}")
print(f" Tests created: {session['tests_written']}")
print(f" Bugs fixed: {session['bugs_fixed']}")
# Efficiency evaluation
if session['duration_minutes'] > 0:
lines_per_minute = session['lines_generated'] / session['duration_minutes']
print(f" Productivity: {lines_per_minute:.1f} lines/min")
def generate_productivity_report(self, days: int = 7) -> str:
"""Generate productivity report"""
cutoff_date = datetime.now() - timedelta(days=days)
recent_sessions = [
s for s in self.metrics["sessions"]
if datetime.fromisoformat(s["start_time"]) > cutoff_date
]
if not recent_sessions:
return f"No session data for the past {days} days"
# Calculate statistics
total_time = sum(s["duration_minutes"] for s in recent_sessions)
total_lines = sum(s["lines_generated"] for s in recent_sessions)
avg_session_time = total_time / len(recent_sessions)
# Tool usage analysis
tool_usage = {}
for session in recent_sessions:
for tool in session["ai_tools_used"]:
tool_usage[tool] = tool_usage.get(tool, 0) + 1
# Generate report
report = f"""
# AI Development Productivity Report (Past {days} Days)
## Overview
- Sessions: {len(recent_sessions)}
- Total development time: {total_time} minutes ({total_time/60:.1f} hours)
- Total lines generated: {total_lines} lines
- Average session time: {avg_session_time:.1f} minutes
## Productivity Metrics
- Lines per hour: {total_lines / (total_time/60):.1f} lines/hour
- Lines per session: {total_lines / len(recent_sessions):.1f} lines/session
## AI Tool Usage
{chr(10).join([f"- {tool}: Used {count} times" for tool, count in sorted(tool_usage.items(), key=lambda x: x[1], reverse=True)])}
## Most Productive Sessions
{self._get_top_sessions(recent_sessions, 3)}
"""
return report.strip()
def _get_top_sessions(self, sessions: List[Dict], top_n: int = 3) -> str:
"""Identify most productive sessions"""
sorted_sessions = sorted(
sessions,
key=lambda s: s["lines_generated"] / max(s["duration_minutes"], 1),
reverse=True
)
result = []
for i, session in enumerate(sorted_sessions[:top_n]):
efficiency = session["lines_generated"] / max(session["duration_minutes"], 1)
result.append(
f"{i+1}. {session['task_description'][:50]}... "
f"({efficiency:.1f} lines/min, {session['duration_minutes']} min)"
)
return '\n'.join(result)
# Usage example
def productivity_tracking_example():
"""Productivity tracking usage example"""
tracker = ProductivityMetrics()
# Start session
session_id = tracker.start_session(
"Implement new API feature",
["Claude 4", "OpenAI Codex"]
)
# Simulate development work
print("💻 Development in progress...")
time.sleep(2) # Simulate actual development time
# End session
result = tracker.end_session(session_id, "completed")
# Generate report
report = tracker.generate_productivity_report(7)
print("\n" + report)
return result
if __name__ == "__main__":
productivity_tracking_example()
Summary¶
- Claude 4: Optimal for long-term tasks and comprehensive analysis, creating complex refactoring plans
- OpenAI Codex: Daily development support and test automation, ease of use through ChatGPT integration
- Google Gemini CLI: Real-time support via terminal integration, security scanning
Related Articles¶
- Must-See for AI Developers: Claude 4 Launch, OpenAI Codex & Terminal Revolution - July 22, 2025 Breaking News (archived)
- Claude Code MCP Practical Guide: Complete Integration Steps for Developers