A simple Python application for making API calls to Anthropic's Claude AI. This project serves as a foundation for learning agentic AI applications and demonstrates basic API integration patterns. I built this mainly for the purpose of experimenting CICD pipelines for Agentic AI Applications with Python.
- Simple API Integration: Easy-to-use wrapper for Anthropic's Claude API
- Multiple Examples: Text analysis, creative writing, and interactive chat
- Conversation Management: Save and manage chat conversations
- Modular Design: Clean separation of concerns for easy extension
- Interactive CLI: Command-line interface for real-time interactions
- Environment Configuration: Secure API key management
anthropic-ai-app/
β
βββ .env # Environment variables (API key)
βββ .gitignore # Git ignore file
βββ requirements.txt # Python dependencies
βββ config.py # Configuration settings
βββ main.py # Main application entry point
βββ README.md # Project documentation
βββ api/
β βββ __init__.py
β βββ anthropic_client.py # Anthropic API client wrapper
βββ examples/
β βββ __init__.py
β βββ simple_chat.py # Basic chat example
β βββ text_analysis.py # Text analysis example
β βββ creative_writing.py # Creative writing example
βββ utils/
βββ __init__.py
βββ helpers.py # Utility functions
- Python 3.7 or higher
- Anthropic API key (get one at console.anthropic.com)
-
Clone the repository:
git clone https://github.com/yourusername/agentic_ai_python_api.git cd anthropic-ai-app
-
Create and activate virtual environment:
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
- Copy
.env.example
to.env
(if provided) or create a new.env
file - Add your Anthropic API key:
ANTHROPIC_API_KEY=your_api_key_here DEFAULT_MODEL=claude-3-5-sonnet-20241022 MAX_TOKENS=1000
- Copy
Start a conversation with Claude:
python main.py chat
Execute all example scripts:
python main.py examples
python examples/simple_chat.py
python examples/text_analysis.py
python examples/creative_writing.py
from api.anthropic_client import AnthropicClient
client = AnthropicClient()
response = client.send_message("Explain quantum computing in simple terms.")
print(response)
from api.anthropic_client import AnthropicClient
client = AnthropicClient()
text = "Your text here..."
response = client.send_message(f"Analyze the sentiment of: {text}")
print(response)
from api.anthropic_client import AnthropicClient
from utils.helpers import create_user_message, create_assistant_message
client = AnthropicClient()
conversation = [
create_user_message("Hello, how are you?"),
create_assistant_message("I'm doing well, thank you!"),
create_user_message("Can you help me with Python?")
]
response = client.chat_conversation(conversation)
print(response)
The application uses environment variables for configuration:
Variable | Description | Default |
---|---|---|
ANTHROPIC_API_KEY |
Your Anthropic API key | Required |
DEFAULT_MODEL |
Claude model to use | claude-3-5-sonnet-20241022 |
MAX_TOKENS |
Maximum tokens in response | 1000 |
Send a single message to Claude.
Parameters:
message
(str): The user messagemodel
(str, optional): Model to usemax_tokens
(int, optional): Maximum response tokenssystem_prompt
(str, optional): System prompt for context
Send a conversation history to Claude.
Parameters:
messages
(List[Dict]): List of message dictionariesmodel
(str, optional): Model to usemax_tokens
(int, optional): Maximum response tokenssystem_prompt
(str, optional): System prompt for context
The client includes basic error handling for:
- Missing API keys
- Network issues
- API rate limits
- Invalid requests
Conversations can be saved to files using the utility functions:
from utils.helpers import save_conversation
# Save conversation to timestamped file
save_conversation(messages)
# Save to specific filename
save_conversation(messages, "my_conversation.txt")
- Create a new file in the
examples/
directory - Import the
AnthropicClient
and utility functions - Follow the existing example patterns
- Add your example to the main examples runner
The AnthropicClient
class can be extended with additional methods for specific use cases:
- Streaming responses
- Function calling
- Custom model parameters
- Response parsing
- Fork the repository
- Create a feature branch:
git checkout -b feature-name
- Make your changes and commit:
git commit -am 'Add feature'
- Push to the branch:
git push origin feature-name
- Submit a pull request
- Never commit your API keys to version control
- The
.env
file is included in.gitignore
for security - Use environment variables for sensitive configuration
This project is licensed under the MIT License - see the LICENSE file for details.
"ANTHROPIC_API_KEY not found"
- Ensure your
.env
file exists and contains your API key - Make sure the
.env
file is in the project root directory
"Module not found" errors
- Activate your virtual environment
- Install dependencies:
pip install -r requirements.txt
API rate limit errors
- Check your API usage at console.anthropic.com
- Implement rate limiting in your application
- Check the Anthropic API documentation
- Review the example files for usage patterns
- Open an issue on GitHub for bugs or questions
This project is part of a 30-day agentic AI bootcamp. Recommended next steps:
- Add streaming responses for real-time chat
- Implement function calling for tool use
- Add memory management for longer conversations
- Build a web interface with Flask/FastAPI
- Explore advanced prompting techniques
- Add monitoring and logging for production use
- v1.0.0 - Initial release with basic API integration and examples
Built with β€οΈ for learning agentic AI applications