Agentic Design Pattern: Code Generation

Written by

in


    """
Pattern 022: Code Generation & Execution

Description:
    The Code Generation & Execution pattern enables agents to generate executable code
    in response to user requests, safely execute the code in a sandboxed environment,
    and return results. This pattern combines LLM code generation capabilities with
    secure execution environments to solve computational problems programmatically.

Components:
    - Code Generator: LLM generates code from natural language
    - Syntax Validator: Checks code for syntax errors
    - Sandbox Environment: Isolated execution context
    - Result Processor: Formats and returns execution results
    - Error Handler: Captures and explains execution errors

Use Cases:
    - Data analysis and visualization
    - Mathematical problem solving
    - Text processing and transformation
    - Algorithm implementation
    - Quick prototyping and testing

LangChain Implementation:
    Uses LLM for code generation with specific prompting strategies,
    implements safe execution with restricted environments, and handles
    multiple programming languages.

Key Features:
    - Multi-language support (Python, JavaScript, etc.)
    - Sandboxed execution for security
    - Syntax and runtime error handling
    - Result capturing and formatting
    - Iterative refinement on errors
"""

import os
import sys
from typing import List, Dict, Any, Optional
from dataclasses import dataclass, field
from enum import Enum
from datetime import datetime
import io
import contextlib
from dotenv import load_dotenv

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

load_dotenv()


class Language(Enum):
    """Supported programming languages."""
    PYTHON = "python"
    JAVASCRIPT = "javascript"


class ExecutionStatus(Enum):
    """Status of code execution."""
    SUCCESS = "success"
    SYNTAX_ERROR = "syntax_error"
    RUNTIME_ERROR = "runtime_error"
    TIMEOUT = "timeout"
    FORBIDDEN = "forbidden"


@dataclass
class CodeSnippet:
    """Generated code snippet."""
    language: Language
    code: str
    description: str
    timestamp: datetime = field(default_factory=datetime.now)


@dataclass
class ExecutionResult:
    """Result of code execution."""
    code: str
    status: ExecutionStatus
    output: Optional[str] = None
    error: Optional[str] = None
    duration: Optional[float] = None
    timestamp: datetime = field(default_factory=datetime.now)


class CodeGenerator:
    """
    Generates code from natural language descriptions.
    """
    
    def __init__(self, model: str = "gpt-3.5-turbo"):
        """
        Initialize code generator.
        
        Args:
            model: LLM model to use
        """
        self.llm = ChatOpenAI(model=model, temperature=0.2)
    
    def generate_python(
        self,
        task: str,
        context: Optional[str] = None
    ) -> CodeSnippet:
        """
        Generate Python code for a task.
        
        Args:
            task: Task description
            context: Optional context or constraints
            
        Returns:
            Generated code snippet
        """
        prompt = ChatPromptTemplate.from_messages([
            ("system", """You are an expert Python programmer. Generate clean, efficient,
and well-commented Python code to accomplish the given task.

Guidelines:
- Write complete, runnable code
- Use standard library when possible
- Include helpful comments
- Handle edge cases
- Use proper error handling
- Print or return results clearly

Respond with ONLY the Python code, no explanations before or after."""),
            ("user", "{task}{context_str}")
        ])
        
        chain = prompt | self.llm | StrOutputParser()
        
        context_str = f"\n\nContext: {context}" if context else ""
        
        response = chain.invoke({
            "task": task,
            "context_str": context_str
        })
        
        # Extract code from response
        code = self._extract_code(response)
        
        return CodeSnippet(
            language=Language.PYTHON,
            code=code,
            description=task
        )
    
    def generate_javascript(
        self,
        task: str,
        context: Optional[str] = None
    ) -> CodeSnippet:
        """
        Generate JavaScript code for a task.
        
        Args:
            task: Task description
            context: Optional context
            
        Returns:
            Generated code snippet
        """
        prompt = ChatPromptTemplate.from_messages([
            ("system", """You are an expert JavaScript programmer. Generate clean, efficient
JavaScript code to accomplish the given task.

Guidelines:
- Write complete, runnable Node.js code
- Use modern ES6+ syntax
- Include helpful comments
- Handle edge cases
- Use console.log for output

Respond with ONLY the JavaScript code, no explanations."""),
            ("user", "{task}{context_str}")
        ])
        
        chain = prompt | self.llm | StrOutputParser()
        
        context_str = f"\n\nContext: {context}" if context else ""
        
        response = chain.invoke({
            "task": task,
            "context_str": context_str
        })
        
        code = self._extract_code(response)
        
        return CodeSnippet(
            language=Language.JAVASCRIPT,
            code=code,
            description=task
        )
    
    def refine_code(
        self,
        original_code: str,
        error: str,
        language: Language
    ) -> str:
        """
        Refine code based on error feedback.
        
        Args:
            original_code: Original code that failed
            error: Error message
            language: Programming language
            
        Returns:
            Refined code
        """
        prompt = ChatPromptTemplate.from_messages([
            ("system", f"""You are debugging {language.value} code. Fix the error in the
provided code and return the corrected version.

Respond with ONLY the corrected code, no explanations."""),
            ("user", """Original Code:
```{language}
{code}
```

Error:
{error}

Provide the corrected code:""")
        ])
        
        chain = prompt | self.llm | StrOutputParser()
        
        response = chain.invoke({
            "language": language.value,
            "code": original_code,
            "error": error
        })
        
        return self._extract_code(response)
    
    def _extract_code(self, response: str) -> str:
        """Extract code from LLM response."""
        # Remove markdown code blocks
        lines = response.split("\n")
        code_lines = []
        in_code_block = False
        
        for line in lines:
            if line.strip().startswith("```"):
                in_code_block = not in_code_block
                continue
            if in_code_block or not line.strip().startswith("```"):
                code_lines.append(line)
        
        code = "\n".join(code_lines).strip()
        
        # If no code blocks found, return entire response
        if not code:
            code = response.strip()
        
        return code


class SandboxExecutor:
    """
    Safely executes code in sandboxed environment.
    """
    
    def __init__(self):
        """Initialize sandbox executor."""
        # Restricted builtins for Python execution
        self.safe_builtins = {
            'abs': abs, 'all': all, 'any': any, 'ascii': ascii,
            'bin': bin, 'bool': bool, 'chr': chr, 'dict': dict,
            'divmod': divmod, 'enumerate': enumerate, 'filter': filter,
            'float': float, 'format': format, 'hex': hex, 'int': int,
            'len': len, 'list': list, 'map': map, 'max': max,
            'min': min, 'oct': oct, 'ord': ord, 'pow': pow,
            'range': range, 'reversed': reversed, 'round': round,
            'set': set, 'slice': slice, 'sorted': sorted, 'str': str,
            'sum': sum, 'tuple': tuple, 'zip': zip,
            'print': print, 'isinstance': isinstance, 'type': type
        }
    
    def execute_python(self, code: str) -> ExecutionResult:
        """
        Execute Python code safely.
        
        Args:
            code: Python code to execute
            
        Returns:
            Execution result
        """
        import time
        
        # Check for forbidden operations
        forbidden_keywords = ['import os', 'import sys', 'eval', 'exec', '__import__', 
                            'open', 'file', 'compile']
        for keyword in forbidden_keywords:
            if keyword in code:
                return ExecutionResult(
                    code=code,
                    status=ExecutionStatus.FORBIDDEN,
                    error=f"Forbidden operation: {keyword}"
                )
        
        # Syntax check
        try:
            compile(code, '', 'exec')
        except SyntaxError as e:
            return ExecutionResult(
                code=code,
                status=ExecutionStatus.SYNTAX_ERROR,
                error=str(e)
            )
        
        # Execute in sandbox
        output_buffer = io.StringIO()
        
        try:
            start_time = time.time()
            
            # Redirect stdout
            with contextlib.redirect_stdout(output_buffer):
                # Create restricted namespace
                namespace = {'__builtins__': self.safe_builtins}
                
                # Execute code
                exec(code, namespace)
            
            duration = time.time() - start_time
            output = output_buffer.getvalue()
            
            return ExecutionResult(
                code=code,
                status=ExecutionStatus.SUCCESS,
                output=output if output else "Code executed successfully (no output)",
                duration=duration
            )
            
        except Exception as e:
            return ExecutionResult(
                code=code,
                status=ExecutionStatus.RUNTIME_ERROR,
                error=f"{type(e).__name__}: {str(e)}"
            )
    
    def execute_javascript(self, code: str) -> ExecutionResult:
        """
        Execute JavaScript code (simulated - would need Node.js).
        
        Args:
            code: JavaScript code
            
        Returns:
            Execution result
        """
        # In real implementation, would execute with Node.js
        # For demonstration, we simulate execution
        
        return ExecutionResult(
            code=code,
            status=ExecutionStatus.SUCCESS,
            output="[Simulated] JavaScript execution would require Node.js runtime",
            duration=0.0
        )


class CodeGenerationAgent:
    """
    Agent that generates and executes code to solve problems.
    """
    
    def __init__(
        self,
        model: str = "gpt-3.5-turbo",
        max_retries: int = 2
    ):
        """
        Initialize code generation agent.
        
        Args:
            model: LLM model to use
            max_retries: Maximum refinement attempts on errors
        """
        self.generator = CodeGenerator(model)
        self.executor = SandboxExecutor()
        self.max_retries = max_retries
        self.execution_history: List[ExecutionResult] = []
    
    def solve_with_code(
        self,
        task: str,
        language: Language = Language.PYTHON,
        context: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        Solve a task by generating and executing code.
        
        Args:
            task: Task description
            language: Programming language to use
            context: Optional context
            
        Returns:
            Solution with code and results
        """
        print(f"\n[Agent] Solving task: {task}")
        print(f"[Agent] Language: {language.value}\n")
        
        # Generate initial code
        print("[Agent] Generating code...")
        if language == Language.PYTHON:
            snippet = self.generator.generate_python(task, context)
        else:
            snippet = self.generator.generate_javascript(task, context)
        
        print(f"\n[Agent] Generated code:\n")
        print("=" * 60)
        print(snippet.code)
        print("=" * 60)
        
        # Execute with retries
        attempts = []
        result = None
        
        for attempt in range(self.max_retries + 1):
            print(f"\n[Agent] Execution attempt {attempt + 1}...")
            
            # Execute code
            if language == Language.PYTHON:
                result = self.executor.execute_python(snippet.code)
            else:
                result = self.executor.execute_javascript(snippet.code)
            
            self.execution_history.append(result)
            attempts.append(result)
            
            # Check if successful
            if result.status == ExecutionStatus.SUCCESS:
                print(f"[Agent] ✓ Success!")
                print(f"\n[Agent] Output:")
                print("-" * 60)
                print(result.output)
                print("-" * 60)
                break
            
            # Handle error
            print(f"[Agent] ✗ {result.status.value}: {result.error}")
            
            if attempt < self.max_retries:
                print(f"[Agent] Refining code...")
                snippet.code = self.generator.refine_code(
                    snippet.code,
                    result.error,
                    language
                )
                print(f"\n[Agent] Refined code:\n")
                print("=" * 60)
                print(snippet.code)
                print("=" * 60)
        
        return {
            "task": task,
            "language": language.value,
            "final_code": snippet.code,
            "result": result,
            "attempts": len(attempts),
            "success": result.status == ExecutionStatus.SUCCESS
        }


def demonstrate_code_generation():
    """Demonstrate the Code Generation & Execution pattern."""
    
    print("=" * 80)
    print("CODE GENERATION & EXECUTION PATTERN DEMONSTRATION")
    print("=" * 80)
    
    agent = CodeGenerationAgent()
    
    # Test 1: Simple calculation
    print("\n" + "=" * 80)
    print("TEST 1: Mathematical Calculation")
    print("=" * 80)
    
    task1 = "Calculate the factorial of 10 and print the result"
    result1 = agent.solve_with_code(task1, Language.PYTHON)
    
    print("\n" + "=" * 80)
    print("SOLUTION SUMMARY")
    print("=" * 80)
    print(f"Success: {result1['success']}")
    print(f"Attempts: {result1['attempts']}")
    
    # Test 2: Data processing
    print("\n" + "=" * 80)
    print("TEST 2: Data Processing")
    print("=" * 80)
    
    task2 = """Create a list of numbers from 1 to 20, filter only even numbers,
square each one, and print the sum of all squared even numbers"""
    result2 = agent.solve_with_code(task2, Language.PYTHON)
    
    print("\n" + "=" * 80)
    print("SOLUTION SUMMARY")
    print("=" * 80)
    print(f"Success: {result2['success']}")
    print(f"Attempts: {result2['attempts']}")
    
    # Test 3: String manipulation
    print("\n" + "=" * 80)
    print("TEST 3: String Manipulation")
    print("=" * 80)
    
    task3 = """Create a function that takes a string and returns a dictionary
counting the frequency of each character. Test it with 'hello world' and print the result."""
    result3 = agent.solve_with_code(task3, Language.PYTHON)
    
    print("\n" + "=" * 80)
    print("SOLUTION SUMMARY")
    print("=" * 80)
    print(f"Success: {result3['success']}")
    print(f"Attempts: {result3['attempts']}")
    
    # Test 4: Algorithm implementation
    print("\n" + "=" * 80)
    print("TEST 4: Algorithm Implementation")
    print("=" * 80)
    
    task4 = """Implement the Fibonacci sequence up to the 15th number using iteration
(not recursion) and print all numbers"""
    result4 = agent.solve_with_code(task4, Language.PYTHON)
    
    print("\n" + "=" * 80)
    print("SOLUTION SUMMARY")
    print("=" * 80)
    print(f"Success: {result4['success']}")
    print(f"Attempts: {result4['attempts']}")
    
    # Summary
    print("\n" + "=" * 80)
    print("SUMMARY")
    print("=" * 80)
    
    successful = sum(1 for r in agent.execution_history if r.status == ExecutionStatus.SUCCESS)
    total = len(agent.execution_history)
    
    print(f"""
Total tasks: 4
Total execution attempts: {total}
Successful executions: {successful}
Success rate: {successful/total*100:.0f}%

Execution Breakdown:
""")
    
    status_counts = {}
    for result in agent.execution_history:
        status_counts[result.status.value] = status_counts.get(result.status.value, 0) + 1
    
    for status, count in status_counts.items():
        print(f"  {status}: {count}")
    
    print("""
The Code Generation & Execution pattern demonstrates:

1. **Natural Language to Code**: Converts descriptions to executable code
2. **Safe Execution**: Sandboxed environment prevents harmful operations
3. **Error Recovery**: Automatically refines code on failures
4. **Multi-Language**: Supports multiple programming languages
5. **Result Capture**: Captures and formats execution output

Key Benefits:
- **Programmatic Problem Solving**: Handles complex computations
- **Safety**: Restricted execution environment
- **Self-Correction**: Learns from execution errors
- **Flexibility**: Works across languages and domains
- **Verifiable**: Actual code execution provides concrete results

Use Cases:
- Data analysis and statistics
- Mathematical problem solving
- Algorithm implementation and testing
- Text processing pipelines
- Quick prototyping and experimentation

Limitations:
- Restricted operations for security
- Limited library access in sandbox
- Execution timeouts for safety
- Language-specific constraints

This pattern enables agents to leverage the full power of programming
to solve computational problems that are difficult for pure LLMs.
""")


if __name__ == "__main__":
    demonstrate_code_generation()

    

Agentic Design Pattern: Code Generation

Pattern 022: Code Generation – Line-by-Line Explanation

Overview

This document provides a detailed line-by-line explanation of the Code Generation pattern implementation.


Pattern Description

Pattern 022: Code Generation & Execution

Description: The Code Generation & Execution pattern enables agents to generate executable code in response to user requests, safely execute the code in a sandboxed environment, and return results. This pattern combines LLM code generation capabilities with secure execution environments to solve computational problems programmatically.

Components: – Code Generator: LLM generates code from natural language – Syntax Validator: Checks code for syntax errors – Sandbox Environment: Isolated execution context – Result Processor: Formats and returns execution results – Error Handler: Captures and explains execution errors

Use Cases: – Data analysis and visualization – Mathematical problem solving – Text processing and transformation – Algorithm implementation – Quick prototyping and testing

LangChain Implementation: Uses LLM for code generation with specific prompting strategies, implements safe execution with restricted environments, and handles multiple programming languages.

Key Features: – Multi-language support (Python, JavaScript, etc.) – Sandboxed execution for security – Syntax and runtime error handling – Result capturing and formatting – Iterative refinement on errors


File Statistics

  • Total Lines: 603
  • Classes: 7
  • Functions: 1
  • Constants: 0

Module Documentation

"""
Pattern 022: Code Generation & Execution

Description:
    The Code Generation & Execution pattern enables agents to generate executable code
    in response to user requests, safely execute the code in a sandboxed environment,
    and return results. This pattern combines LLM code generation capabilities with
    secure execution environments to solve computational problems programmatically.

Components:
    - Code Generator: LLM generates code from natural language
    - Syntax Validator: Checks code for syntax errors
    - Sandbox Environment: Isolated execution context
    - Result Processor: Formats and returns execution results
    - Error Handler: Captures and explains execution errors

Use Cases:
    - Data analysis and visualization
    - Mathematical problem solving
    - Text processing and transformation
    - Algorithm implementation
    - Quick prototyping and testing

LangChain Implementation:
    Uses LLM for code generation with specific prompting strategies,
    implements safe execution with restricted environments, and handles
    multiple programming languages.

Key Features:
    - Multi-language support (Python, JavaScript, etc.)
    - Sandboxed execution for security
    - Syntax and runtime error handling
    - Result capturing and formatting
    - Iterative refinement on errors
"""

Purpose: This module implements the Code Generation pattern, providing a structured approach to agent design and implementation.


Import Statements

Standard Library Imports

import dataclasses.dataclass
import dataclasses.field
import datetime.datetime
import enum.Enum
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import os
import sys
import time
import typing.Any
import typing.Dict
import typing.List
import typing.Optional

Purpose: Core Python functionality: – dataclasses.dataclass: Simplified class definitions with @dataclass – dataclasses.field: Simplified class definitions with @dataclass – datetime.datetime: Date and time handling – enum.Enum: Enumeration support – langchain_core.output_parsers.StrOutputParser: Regular expression operations – langchain_core.prompts.ChatPromptTemplate: Regular expression operations – os: Environment variable access and system operations – time: Time-related functions – typing.Any: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional) – typing.List: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Optional: Type hints for better code documentation (List, Dict, Any, Optional)

LangChain Imports

from langchain_openai.ChatOpenAI

Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows

Other Imports

from contextlib
from dotenv.load_dotenv
from io

Purpose: Additional dependencies for specialized functionality.


Classes

Class: Language

Lines 54-57

Inherits from: Enum

Purpose:

Supported programming languages.

Class: ExecutionStatus

Lines 60-66

Inherits from: Enum

Purpose:

Status of code execution.

Class: CodeSnippet

Lines 70-75

Purpose:

Generated code snippet.

Class: ExecutionResult

Lines 79-86

Purpose:

Result of code execution.

Class: CodeGenerator

Lines 89-262

Purpose:

Generates code from natural language descriptions.

Methods:

__init__(self, model)

Lines 94-101

Purpose:

Initialize code generator.

Args:
    model: LLM model to use

Parameters: – model: Language model instance

generate_python(self, task, context)

Lines 103-150

Purpose:

Generate Python code for a task.

Args:
    task: Task description
    context: Optional context or constraints

Returns:
    Generated code snippet

Parameters: – task: Task – context: Context

Returns: CodeSnippet

generate_javascript(self, task, context)

Lines 152-197

Purpose:

Generate JavaScript code for a task.

Args:
    task: Task description
    context: Optional context

Returns:
    Generated code snippet

Parameters: – task: Task – context: Context

Returns: CodeSnippet

refine_code(self, original_code, error, language)

Lines 199-240

Purpose:

Refine code based on error feedback.

Args:
    original_code: Original code that failed
    error: Error message
    language: Programming language

Returns:
    Refined code

Parameters: – original_code: Original code – error: Error – language: Language

Returns: str

_extract_code(self, response)

Lines 242-262

Purpose:

Extract code from LLM response.

Parameters: – response: Response

Returns: str


Class: SandboxExecutor

Lines 265-368

Purpose:

Safely executes code in sandboxed environment.

Methods:

__init__(self)

Lines 270-284

Purpose:

Initialize sandbox executor.

Parameters:

execute_python(self, code)

Lines 286-348

Purpose:

Execute Python code safely.

Args:
    code: Python code to execute

Returns:
    Execution result

Parameters: – code: Code

Returns: ExecutionResult

execute_javascript(self, code)

Lines 350-368

Purpose:

Execute JavaScript code (simulated - would need Node.js).

Args:
    code: JavaScript code

Returns:
    Execution result

Parameters: – code: Code

Returns: ExecutionResult


Class: CodeGenerationAgent

Lines 371-472

Purpose:

Agent that generates and executes code to solve problems.

Methods:

__init__(self, model, max_retries)

Lines 376-391

Purpose:

Initialize code generation agent.

Args:
    model: LLM model to use
    max_retries: Maximum refinement attempts on errors

Parameters: – model: Language model instance – max_retries: Max retries

solve_with_code(self, task, language, context)

Lines 393-472

Purpose:

Solve a task by generating and executing code.

Args:
    task: Task description
    language: Programming language to use
    context: Optional context

Returns:
    Solution with code and results

Parameters: – task: Task – language: Language – context: Context

Returns: <ast.Subscript object at 0x000001A06D608890>


Functions

demonstrate_code_generation()

Lines 475-598

Purpose:

Demonstrate the Code Generation & Execution pattern.

Key Concepts Summary

Pattern Overview

The Code Generation pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.

Core Components

  1. Agent Class: Main implementation of the pattern logic
  2. Prompt Engineering: Carefully crafted prompts to guide LLM behavior
  3. Tool Integration: Optional tools for extended functionality
  4. Memory Management: State handling across interactions
  5. Output Parsing: Structured response processing

When to Use

✅ Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability

❌ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)


Best Practices Demonstrated

1. Environment Configuration

  • ✅ Use .env files for API keys
  • ✅ Never hardcode credentials
  • ✅ Validate environment variables on startup

2. Error Handling

  • ✅ Graceful degradation on failures
  • ✅ Informative error messages
  • ✅ Logging for debugging

3. Prompt Engineering

  • ✅ Clear, specific instructions
  • ✅ Structured output formats
  • ✅ Few-shot examples when beneficial

4. Code Organization

  • ✅ Separation of concerns (class-based design)
  • ✅ Type hints for clarity
  • ✅ Docstrings for documentation

5. Performance Optimization

  • ✅ Appropriate temperature settings
  • ✅ Token budget management
  • ✅ Caching where applicable

LangChain Components Used

  • ChatOpenAI: OpenAI model interface
  • Chains: Sequential component composition

Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development


Execution Flow

1. Initialize agent with configuration
   ↓
2. Load environment and dependencies
   ↓
3. Prepare prompt with input data
   ↓
4. Invoke LLM through LangChain
   ↓
5. Parse and validate response
   ↓
6. Return formatted result

Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation


Performance Considerations

Computational Cost

  • LLM Calls: Primary cost factor (based on tokens)
  • Latency: Depends on model and input size
  • Memory: Minimal for most patterns

Optimization Strategies

  1. Prompt Optimization: Minimize tokens while maintaining clarity
  2. Caching: Cache repeated queries/responses
  3. Model Selection: Use smaller models when appropriate
  4. Batch Processing: Group similar requests
  5. Streaming: For long responses, stream tokens

Cost Estimates (Approximate)

  • GPT-3.5-turbo: ~$0.002 per request
  • GPT-4: ~$0.03 per request
  • GPT-4-turbo: ~$0.01 per request

Actual costs vary based on input/output token counts


Common Pitfalls to Avoid

1. Prompt Design Issues

❌ Vague or ambiguous instructions
✅ Clear, specific, testable prompts

2. Error Handling

❌ Ignoring API errors or rate limits
✅ Implement retry logic and graceful degradation

3. Token Management

❌ Exceeding model token limits
✅ Truncate inputs, chunk processing

4. Security

❌ Exposing API keys in code
✅ Use environment variables

5. Testing

❌ Only testing happy paths
✅ Test edge cases, failures, malformed inputs

6. Cost Management

❌ Unmonitored API usage
✅ Implement budgets, alerts, and monitoring


Further Improvements

Production Enhancements

  1. Monitoring: Add logging, metrics, tracing
  2. Validation: Input/output schema validation
  3. Rate Limiting: Implement request throttling
  4. Caching: Redis or in-memory cache for responses
  5. Async Support: Non-blocking operations for scalability

Feature Extensions

  1. Multi-Model Support: Switch between providers
  2. Fallback Models: Graceful degradation strategy
  3. Custom Tools: Domain-specific function calling
  4. Advanced Memory: Vector stores for long-term memory
  5. Evaluation: Automated testing and benchmarking

Code Quality

  1. Testing: Unit tests, integration tests
  2. Documentation: API docs, usage examples
  3. Type Safety: Stricter type hints
  4. Configuration: Externalized config files

Related Patterns

  • Pattern 021
  • Pattern 023
  • Other Tool Use Patterns (020-025)

Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…


Summary

The Code Generation pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:

✅ Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns

🎯 Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability

📚 Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/


File: 022_code_generation.py
Total Lines: 603
Pattern Category: AI Agent Design Pattern #022


Generated from actual implementation code
For educational and reference purposes

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *