Go to file
Ariel Frischer 32fcf914ed
Set Default Max Token Limit with Provider/Model Dictionary and Limit Tokens for Anthropic Claude React Agent (#45)
* feat(agent_utils.py): add get_model_token_limit function to retrieve token limits for models based on provider and model name
feat(models_tokens.py): create models_tokens module to store token limits for various models and providers
test(agent_utils.py): implement unit tests for get_model_token_limit and create_agent functions to ensure correct behavior and error handling

* test: Add unit tests for token limiting and agent creation functionality

* fix: Correct indentation and add missing test function for error handling

* fix: Update test assertion to use messages_modifier instead of state_modifier

* feat(agent_utils.py): add limit_tokens function to manage message token limits and preserve system message
fix(agent_utils.py): update get_model_token_limit to handle exceptions and return None on error
fix(ciayn_agent.py): set default max_tokens to DEFAULT_TOKEN_LIMIT in CiaynAgent initialization
feat(models_tokens.py): define DEFAULT_TOKEN_LIMIT for consistent token limit management across agents
style(agent_utils.py): improve code formatting and consistency in function definitions and comments
style(agent_utils.py): refactor imports for better organization and readability
fix(test_agent_utils.py): correct test assertion to use state_modifier instead of messages_modifier for create_agent function

* refactor(agent_utils.py): improve docstring clarity and formatting for limit_tokens function to enhance readability
refactor(test_agent_utils.py): format test assertions for consistency and readability in agent creation tests

* feat: Update limit_tokens function to support Dict type for state parameter

* feat: Update limit_tokens to handle both list and dict input types

* refactor: Extract duplicate token trimming logic into helper function

* refactor: Rename and update message trimming functions for clarity

* refactor: Extract agent kwargs logic into a helper method for reuse

* refactor: Rename _build_agent_kwargs to build_agent_kwargs for clarity

* fix: Ensure state_modifier is passed correctly for agent creation

* test: Add tests for create_agent token limiting configuration

* refactor: Simplify CiaynAgent instantiation to only use max_tokens parameter

* refactor: Remove is_react_agent parameter from build_agent_kwargs function

* test: Fix test assertions for state_modifier in agent creation tests

* fix: Update agent creation to handle checkpointer and simplify tests

* test: Remove unnecessary assertions from agent creation test

* feat: Implement token limiting configuration for create_agent function

* refactor: Remove unused model info and token limit retrieval code

* test: Fix assertion errors in agent creation tests and update state_modifier handling

* test: Remove commented-out code and clarify assertions in tests

* test: Fix assertion in test_create_agent_anthropic_token_limiting_disabled

* feat(main.py): add --limit-tokens argument to control token limiting in agent state
fix(main.py): include limit_tokens in configuration to ensure proper state management

* test: Refactor agent creation tests for improved readability and consistency

* test: Modify error handling in create_agent test to use side_effect on get_model_token_limit

* test: Improve error handling in create_agent test to verify fallback behavior

* test: Trigger exception on get_model_token_limit in error handling test

* refactor(agent_utils.py): remove unused config parameter from create_agent function to simplify the function signature
fix(agent_utils.py): ensure config is always retrieved from _global_memory with a default value to prevent potential errors
test(tests/test_agent_utils.py): remove outdated test for create_agent error handling to clean up the test suite

* feat: Add debug print for agent_kwargs in create_agent function

* refactor: Replace lambda with inner function for state_modifier in agent_utils

* refactor: Simplify limit_tokens function to return only message sequences

* feat: Add debug print statements to show token trimming details in trim_messages

* PAIN

* feat(main.py): add debug print statement for args.chat to assist in troubleshooting chat mode
feat(agent_utils.py): implement estimate_messages_tokens function to calculate total tokens in messages
refactor(agent_utils.py): replace token counting logic in trim_messages_with_removal with estimate_messages_tokens for clarity
refactor(agent_utils.py): modify state_modifier to accept model and max_tokens parameters for better flexibility
refactor(agent_utils.py): update build_agent_kwargs to pass model to state_modifier for improved functionality

* feat: Add .direnvrc to manage Python virtual environment activation

* refactor: Update state_modifier to handle first message token count and trim messages

* chore: remove unused .direnvrc file to clean up project structure
feat: add .envrc to .gitignore to prevent environment configuration file from being tracked
fix: update help text for --disable-limit-tokens argument for clarity
refactor: clean up imports in agent_utils.py for better readability
refactor: remove unused functions and comments in agent_utils.py to streamline code
test: add unit tests for state_modifier function to ensure correct message trimming behavior

* refactor: Remove commented-out code in create_agent function

* feat: Add is_anthropic_claude method to check provider and model name

* fix: Correct search/replace block to match existing lines in agent_utils.py

* fix(main.py): update help text for --disable-limit-tokens argument to clarify it applies to react agents
refactor(agent_utils.py): streamline token limit retrieval and improve readability by removing redundant checks and restructuring code
refactor(agent_utils.py): modify build_agent_kwargs to use is_anthropic_claude for clarity and maintainability

* test: Update tests to pass config argument to get_model_token_limit()

* refactor(agent_utils.py): remove unnecessary print statements and improve function signatures for clarity
refactor(agent_utils.py): consolidate provider and model_name retrieval into config parameter for better maintainability

* test: Remove redundant token limiting tests from agent creation logic

* test: Refactor test_state_modifier to use mock_messages fixture

* test(tests): update test description for clarity on state_modifier behavior and use mock_messages for assertions to ensure consistency

* refactor(agent_utils.py): simplify token limit retrieval by removing unnecessary variable initialization and defaulting to None in get method

* chore(main.py): remove debug print statement for args.chat to clean up output
2025-01-20 14:41:29 -05:00
.githooks FIX SyntaxError: f-string expression part cannot include a backslash (#25) 2024-12-30 08:45:21 -05:00
.github/workflows add twine to workflow 2024-12-17 08:51:52 -05:00
assets Added web research demo. 2024-12-24 08:56:40 -05:00
ra_aid Set Default Max Token Limit with Provider/Model Dictionary and Limit Tokens for Anthropic Claude React Agent (#45) 2025-01-20 14:41:29 -05:00
scripts SWEBench updates. 2024-12-31 08:51:21 -05:00
tests Set Default Max Token Limit with Provider/Model Dictionary and Limit Tokens for Anthropic Claude React Agent (#45) 2025-01-20 14:41:29 -05:00
.gitignore Set Default Max Token Limit with Provider/Model Dictionary and Limit Tokens for Anthropic Claude React Agent (#45) 2025-01-20 14:41:29 -05:00
.replit Replit (#16) 2024-12-26 12:21:45 -05:00
CHANGELOG.md Integrate project info into chat prompt. 2025-01-09 15:01:39 -05:00
LICENSE Initial commit 2024-12-10 19:01:20 -05:00
MANIFEST.in Initial commit 2024-12-10 19:01:20 -05:00
Makefile FEAT add test coverage report (#28) 2025-01-01 09:36:03 -05:00
README.md Adding Gemini API due to openrouter's limitations. (#34) 2025-01-04 07:19:05 -05:00
pyproject.toml Adding Gemini API due to openrouter's limitations. (#34) 2025-01-04 07:19:05 -05:00
replit.nix Replit (#16) 2024-12-26 12:21:45 -05:00
requirements-dev.txt FEAT add test coverage report (#28) 2025-01-01 09:36:03 -05:00

README.md


██▀███   ▄▄▄            ▄▄▄       ██▓▓█████▄
▓██ ▒ ██▒▒████▄         ▒████▄    ▓██▒▒██▀ ██▌
▓██ ░▄█ ▒▒██  ▀█▄       ▒██  ▀█▄  ▒██▒░██   █▌
▒██▀▀█▄  ░██▄▄▄▄██      ░██▄▄▄▄██ ░██░░▓█▄   ▌
░██▓ ▒██▒ ▓█   ▓██▒ ██▓  ▓█   ▓██▒░██░░▒████▓
░ ▒▓ ░▒▓░ ▒▒   ▓▒█░ ▒▓▒  ▒▒   ▓▒█░░▓   ▒▒▓  ▒
 ░▒ ░ ▒░  ▒   ▒▒ ░ ░▒    ▒   ▒▒ ░ ▒ ░ ░ ▒  ▒
 ░░   ░   ░   ▒    ░     ░   ▒    ▒ ░ ░ ░  ░
  ░           ░  ░  ░        ░  ░ ░     ░
                    ░                 ░

Python Versions License Status

RA.Aid

AI software development agent powered by aider and advanced reasoning models like o1.

RA.Aid (ReAct Aid) was made by putting aider (https://aider.chat/) in a LangChain ReAct agent loop. This unique combination allows developers to leverage aider's code editing capabilities while benefiting from LangChain's agent-based task execution framework. The tool provides an intelligent assistant that can help with research, planning, and implementation of multi-step development tasks.

RA.Aid is a practical tool for everyday software development and is used for developing real-world applications.

Here's a demo of RA.Aid adding a feature to itself:

RA.Aid Demo

👋 Pull requests are very welcome! Have ideas for how to impove RA.Aid? Don't be shy - your help makes a real difference!

💬 Join our Discord community: Click here to join

⚠️ IMPORTANT: USE AT YOUR OWN RISK ⚠️

  • This tool can and will automatically execute shell commands and make code changes
  • The --cowboy-mode flag can be enabled to skip shell command approval prompts
  • No warranty is provided, either express or implied
  • Always use in version-controlled repositories
  • Review proposed changes in your git diff before committing

Key Features

  • Multi-Step Task Planning: The agent breaks down complex tasks into discrete, manageable steps and executes them sequentially. This systematic approach ensures thorough implementation and reduces errors.

  • Automated Command Execution: The agent can run shell commands automatically to accomplish tasks. While this makes it powerful, it also means you should carefully review its actions.

  • Ability to Leverage Expert Reasoning Models: The agent can use advanced reasoning models such as OpenAI's o1 just when needed, e.g. to solve complex debugging problems or in planning for complex feature implementation.

  • Web Research Capabilities: Leverages Tavily API for intelligent web searches to enhance research and gather real-world context for development tasks

  • Three-Stage Architecture:

    1. Research: Analyzes codebases and gathers context
    2. Planning: Breaks down tasks into specific, actionable steps
    3. Implementation: Executes each planned step sequentially

What sets RA.Aid apart is its ability to handle complex programming tasks that extend beyond single-shot code edits. By combining research, strategic planning, and implementation into a cohesive workflow, RA.Aid can:

  • Break down and execute multi-step programming tasks
  • Research and analyze complex codebases to answer architectural questions
  • Plan and implement significant code changes across multiple files
  • Provide detailed explanations of existing code structure and functionality
  • Execute sophisticated refactoring operations with proper planning

Features

  • Three-Stage Architecture: The workflow consists of three powerful stages:

    1. Research 🔍 - Gather and analyze information
    2. Planning 📋 - Develop execution strategy
    3. Implementation - Execute the plan with AI assistance

    Each stage is powered by dedicated AI agents and specialized toolsets.

  • Advanced AI Integration: Built on LangChain and leverages the latest LLMs for natural language understanding and generation.

  • Human-in-the-Loop Interaction: Optional mode that enables the agent to ask you questions during task execution, ensuring higher accuracy and better handling of complex tasks that may require your input or clarification

  • Comprehensive Toolset:

    • Shell command execution
    • Expert querying system
    • File operations and management
    • Memory management
    • Research and planning tools
    • Code analysis capabilities
  • Interactive CLI Interface: Simple yet powerful command-line interface for seamless interaction

  • Modular Design: Structured as a Python package with specialized modules for console output, processing, text utilities, and tools

  • Git Integration: Built-in support for Git operations and repository management

Installation

RA.Aid can be installed directly using pip:

pip install ra-aid

Prerequisites

Before using RA.Aid, you'll need:

  1. Python package aider installed and available in your PATH:
pip install aider-chat
  1. API keys for the required AI services:
# Set up API keys based on your preferred provider:

# For Anthropic Claude models (recommended)
export ANTHROPIC_API_KEY=your_api_key_here

# For OpenAI models
export OPENAI_API_KEY=your_api_key_here

# For OpenRouter provider (optional)
export OPENROUTER_API_KEY=your_api_key_here

# For OpenAI-compatible providers (optional)
export OPENAI_API_BASE=your_api_base_url

# For Gemini provider (optional)
export GEMINI_API_KEY=your_api_key_here

# For web research capabilities
export TAVILY_API_KEY=your_api_key_here

Note: The programmer tool (aider) will automatically select its model based on your available API keys:

  • If ANTHROPIC_API_KEY is set, it will use Claude models
  • If only OPENAI_API_KEY is set, it will use OpenAI models
  • You can set multiple API keys to enable different features

You can get your API keys from:

Usage

RA.Aid is designed to be simple yet powerful. Here's how to use it:

# Basic usage
ra-aid -m "Your task or query here"

# Research-only mode (no implementation)
ra-aid -m "Explain the authentication flow" --research-only

# Enable verbose logging for detailed execution information
ra-aid -m "Add new feature" --verbose

Command Line Options

  • -m, --message: The task or query to be executed (required)
  • --research-only: Only perform research without implementation
  • --cowboy-mode: Skip interactive approval for shell commands
  • --hil, -H: Enable human-in-the-loop mode, allowing the agent to interactively ask you questions during task execution
  • --provider: Specify the model provider (See Model Configuration section)
  • --model: Specify the model name (See Model Configuration section)
  • --expert-provider: Specify the provider for the expert tool (defaults to OpenAI)
  • --expert-model: Specify the model name for the expert tool (defaults to o1 for OpenAI)
  • --chat: Enable chat mode for interactive assistance
  • --verbose: Enable detailed logging output for debugging and monitoring

Example Tasks

  1. Code Analysis:

    ra-aid -m "Explain how the authentication middleware works" --research-only
    
  2. Complex Changes:

    ra-aid -m "Refactor the database connection code to use connection pooling" --cowboy-mode
    
  3. Automated Updates:

    ra-aid -m "Update deprecated API calls across the entire codebase" --cowboy-mode
    
  4. Code Research:

    ra-aid -m "Analyze the current error handling patterns" --research-only
    
  5. Code Research:

    ra-aid -m "Explain how the authentication middleware works" --research-only
    
  6. Refactoring:

    ra-aid -m "Refactor the database connection code to use connection pooling" --cowboy-mode
    

Human-in-the-Loop Mode

Enable interactive mode to allow the agent to ask you questions during task execution:

ra-aid -m "Implement a new feature" --hil
# or
ra-aid -m "Implement a new feature" -H

This mode is particularly useful for:

  • Complex tasks requiring human judgment
  • Clarifying ambiguous requirements
  • Making architectural decisions
  • Validating critical changes
  • Providing domain-specific knowledge

Web Research

RA.Aid Demo

The agent features autonomous web research capabilities powered by the Tavily API, seamlessly integrating real-world information into its problem-solving workflow. Web research is conducted automatically when the agent determines additional context would be valuable - no explicit configuration required.

For example, when researching modern authentication practices or investigating new API requirements, the agent will autonomously:

  • Search for current best practices and security recommendations
  • Find relevant documentation and technical specifications
  • Gather real-world implementation examples
  • Stay updated on latest industry standards

While web research happens automatically as needed, you can also explicitly request research-focused tasks:

# Focused research task with web search capabilities
ra-aid -m "Research current best practices for API rate limiting" --research-only

Make sure to set your TAVILY_API_KEY environment variable to enable this feature.

Chat Mode

Chat Mode Demo

Enable with --chat to transform ra-aid into an interactive assistant that guides you through research and implementation tasks. Have a natural conversation about what you want to build, explore options together, and dispatch work - all while maintaining context of your discussion. Perfect for when you want to think through problems collaboratively rather than just executing commands.

Command Interruption and Feedback

Command Interrupt Demo

You can interrupt the agent at any time by pressing Ctrl-C. This pauses the agent, allowing you to provide feedback, adjust your instructions, or steer the execution in a new direction. Press Ctrl-C again if you want to completely exit the program.

Shell Command Automation with Cowboy Mode 🏇

The --cowboy-mode flag enables automated shell command execution without confirmation prompts. This is useful for:

  • CI/CD pipelines
  • Automated testing environments
  • Batch processing operations
  • Scripted workflows
ra-aid -m "Update all deprecated API calls" --cowboy-mode

⚠️ Important Safety Notes:

  • Cowboy mode skips confirmation prompts for shell commands
  • Always use in version-controlled repositories
  • Ensure you have a clean working tree before running
  • Review changes in git diff before committing

Model Configuration

RA.Aid supports multiple AI providers and models. The default model is Anthropic's Claude 3 Sonnet (claude-3-5-sonnet-20241022).

The programmer tool (aider) automatically selects its model based on your available API keys. It will use Claude models if ANTHROPIC_API_KEY is set, or fall back to OpenAI models if only OPENAI_API_KEY is available.

Note: The expert tool can be configured to use different providers (OpenAI, Anthropic, OpenRouter, Gemini) using the --expert-provider flag along with the corresponding EXPERT_*API_KEY environment variables. Each provider requires its own API key set through the appropriate environment variable.

Environment Variables

RA.Aid supports multiple providers through environment variables:

  • ANTHROPIC_API_KEY: Required for the default Anthropic provider
  • OPENAI_API_KEY: Required for OpenAI provider
  • OPENROUTER_API_KEY: Required for OpenRouter provider
  • OPENAI_API_BASE: Required for OpenAI-compatible providers along with OPENAI_API_KEY
  • GEMINI_API_KEY: Required for Gemini provider

Expert Tool Environment Variables:

  • EXPERT_OPENAI_API_KEY: API key for expert tool using OpenAI provider
  • EXPERT_ANTHROPIC_API_KEY: API key for expert tool using Anthropic provider
  • EXPERT_OPENROUTER_API_KEY: API key for expert tool using OpenRouter provider
  • EXPERT_OPENAI_API_BASE: Base URL for expert tool using OpenAI-compatible provider
  • EXPERT_GEMINI_API_KEY: API key for expert tool using Gemini provider

You can set these permanently in your shell's configuration file (e.g., ~/.bashrc or ~/.zshrc):

# Default provider (Anthropic)
export ANTHROPIC_API_KEY=your_api_key_here

# For OpenAI features and expert tool
export OPENAI_API_KEY=your_api_key_here

# For OpenRouter provider
export OPENROUTER_API_KEY=your_api_key_here

# For OpenAI-compatible providers
export OPENAI_API_BASE=your_api_base_url

# For Gemini provider
export GEMINI_API_KEY=your_api_key_here

Custom Model Examples

  1. Using Anthropic (Default)

    # Uses default model (claude-3-5-sonnet-20241022)
    ra-aid -m "Your task"
    
    # Or explicitly specify:
    ra-aid -m "Your task" --provider anthropic --model claude-3-5-sonnet-20241022
    
  2. Using OpenAI

    ra-aid -m "Your task" --provider openai --model gpt-4o
    
  3. Using OpenRouter

    ra-aid -m "Your task" --provider openrouter --model mistralai/mistral-large-2411
    
  4. Configuring Expert Provider

    The expert tool is used by the agent for complex logic and debugging tasks. It can be configured to use different providers (OpenAI, Anthropic, OpenRouter, Gemini, openai-compatible) using the --expert-provider flag along with the corresponding EXPERT_*API_KEY environment variables.

    # Use Anthropic for expert tool
    export EXPERT_ANTHROPIC_API_KEY=your_anthropic_api_key
    ra-aid -m "Your task" --expert-provider anthropic --expert-model claude-3-5-sonnet-20241022
    
    # Use OpenRouter for expert tool
    export OPENROUTER_API_KEY=your_openrouter_api_key
    ra-aid -m "Your task" --expert-provider openrouter --expert-model mistralai/mistral-large-2411
    
    # Use default OpenAI for expert tool
    export EXPERT_OPENAI_API_KEY=your_openai_api_key
    ra-aid -m "Your task" --expert-provider openai --expert-model o1
    
    # Use Gemini for expert tool
    export EXPERT_GEMINI_API_KEY=your_gemini_api_key
    ra-aid -m "Your task" --expert-provider gemini --expert-model gemini-2.0-flash-thinking-exp-1219
    

Aider specific Environment Variables you can add:

  • AIDER_FLAGS: Optional comma-separated list of flags to pass to the underlying aider tool (e.g., "yes-always,dark-mode")
# Optional: Configure aider behavior
export AIDER_FLAGS="yes-always,dark-mode,no-auto-commits"

Note: For AIDER_FLAGS, you can specify flags with or without the leading --. Multiple flags should be comma-separated, and spaces around flags are automatically handled. For example, both "yes-always,dark-mode" and "--yes-always, --dark-mode" are valid.

Important Notes:

  • Performance varies between models. The default Claude 3 Sonnet model currently provides the best and most reliable results.
  • Model configuration is done via command line arguments: --provider and --model
  • The --model argument is required for all providers except Anthropic (which defaults to claude-3-5-sonnet-20241022)

Architecture

RA.Aid implements a three-stage architecture for handling development and research tasks:

  1. Research Stage:

    • Gathers information and context
    • Analyzes requirements
    • Identifies key components and dependencies
  2. Planning Stage:

    • Develops detailed implementation plans
    • Breaks down tasks into manageable steps
    • Identifies potential challenges and solutions
  3. Implementation Stage:

    • Executes planned tasks
    • Generates code or documentation
    • Performs necessary system operations

Core Components

  • Console Module (console/): Handles console output formatting and user interaction
  • Processing Module (proc/): Manages interactive processing and workflow control
  • Text Module (text/): Provides text processing and manipulation utilities
  • Tools Module (tools/): Contains various utility tools for file operations, search, and more

Dependencies

Core Dependencies

  • langchain-anthropic: LangChain integration with Anthropic's Claude
  • tavily-python: Tavily API client for web research
  • langgraph: Graph-based workflow management
  • rich>=13.0.0: Terminal formatting and output
  • GitPython==3.1.41: Git repository management
  • fuzzywuzzy==0.18.0: Fuzzy string matching
  • python-Levenshtein==0.23.0: Fast string matching
  • pathspec>=0.11.0: Path specification utilities

Development Dependencies

  • pytest>=7.0.0: Testing framework
  • pytest-timeout>=2.2.0: Test timeout management

Development Setup

  1. Clone the repository:
git clone https://github.com/ai-christianson/RA.Aid.git
cd RA.Aid
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`
  1. Install development dependencies:
pip install -r requirements-dev.txt
  1. Run tests:
python -m pytest

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch:
git checkout -b feature/your-feature-name
  1. Make your changes and commit:
git commit -m 'Add some feature'
  1. Push to your fork:
git push origin feature/your-feature-name
  1. Open a Pull Request

Guidelines

  • Follow PEP 8 style guidelines
  • Add tests for new features
  • Update documentation as needed
  • Keep commits focused and message clear
  • Ensure all tests pass before submitting PR

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Copyright (c) 2024 AI Christianson

Contact