CLAIF provider for Anthropic Claude Code CLI - seamless Claude integration for the Command-Line AI Framework
Project description
claif_cla
Claif provider for Anthropic's Claude Code CLI
Quickstart
claif_cla is a Python wrapper for Claude that provides session management, tool approval strategies, and a rich CLI interface. Version 1.0.8 improves message type handling between claude-code-sdk and Claif formats. Install it and start chatting with Claude in seconds:
pip install claif_cla && python -m claif_cla.cli ask "Hello, Claude!"
claif_cla is a Python package that provides a thin wrapper around the claude_code_sdk package, integrating Anthropic's Claude Code CLI into the Claif (Command-Line AI Framework) ecosystem.
What is claif_cla?
This package acts as a bridge between the Claif framework and Claude's AI capabilities. It provides a minimal interface that:
- Wraps
claude_code_sdkwith Claif-standard options - Manages persistent conversation sessions
- Implements flexible tool approval strategies for MCP (Model Context Protocol) tools
- Provides response caching to reduce API costs
- Offers a rich CLI interface built with Fire and Rich libraries
Installation
From PyPI
pip install claif_cla
From Source
git clone https://github.com/twardoch/claif_cla.git
cd claif_cla
pip install -e .
Development Installation
pip install -e ".[dev,test]"
Command Line Usage
The package provides a Fire-based CLI interface. Run commands through Python:
python -m claif_cla.cli --help
Basic Commands
# Ask Claude a question
python -m claif_cla.cli ask "What is the theory of relativity?"
# Stream responses in real-time
python -m claif_cla.cli stream "Tell me about machine learning"
# Interactive conversation mode
python -m claif_cla.cli interactive
# Check service health
python -m claif_cla.cli health
Session Management
# List sessions
python -m claif_cla.cli session list
# Create new session
python -m claif_cla.cli session create
# Show session messages
python -m claif_cla.cli session show SESSION_ID
# Export session
python -m claif_cla.cli session export SESSION_ID --format markdown --output chat.md
Python API Usage
Basic Query
import asyncio
from claif_cla import query
from claif.common import ClaifOptions
async def main():
# Simple query
async for message in query("Hello, Claude!"):
print(f"{message.role}: {message.content}")
# Query with options
options = ClaifOptions(
model="claude-3-opus-20240229",
temperature=0.7,
max_tokens=500
)
async for message in query("Explain Python decorators", options):
print(message.content)
asyncio.run(main())
Session Management
from claif_cla.session import SessionManager, Session
from claif.common import Message, MessageRole
# Initialize session manager
session_mgr = SessionManager()
# Create session
session_id = session_mgr.create_session()
# Add messages
message = Message(
role=MessageRole.USER,
content="What is machine learning?"
)
session_mgr.add_message(session_id, message)
# Export session
markdown = session_mgr.export_session(session_id, export_format="markdown")
Tool Approval Strategies
from claif_cla.approval import create_approval_strategy
# Allow specific tools only
safe_tools = create_approval_strategy("allow_list", {
"allowed_tools": ["read_file", "list_files", "search"]
})
# Deny dangerous tools
deny_dangerous = create_approval_strategy("deny_list", {
"denied_tools": ["delete_file", "execute_command"]
})
# Pattern-based approval
patterns = create_approval_strategy("pattern", {
"patterns": ["read_.*", "list_.*"],
"deny": False
})
Why Use claif_cla?
- Minimal Overhead: Thin wrapper design adds minimal complexity to claude-code-sdk
- Session Persistence: Save and restore conversations across sessions
- Tool Control: Fine-grained control over MCP tool approval
- Response Caching: Built-in caching reduces API costs
- Claif Integration: Works seamlessly with other Claif providers
How It Works
Architecture
The package consists of five main modules:
claif_cla/
├── __init__.py # Main query function and exports
├── cli.py # Fire-based CLI interface
├── wrapper.py # Enhanced wrapper with caching and retry logic
├── session.py # Session management and persistence
└── approval.py # MCP tool approval strategies
Core Components
-
Main Module (
__init__.py)- Provides the
queryfunction that converts Claif options to Claude options - Thin wrapper around
claude_code_sdk.query - Uses loguru for logging
- Provides the
-
CLI Module (
cli.py)- Fire-based command-line interface
- Commands: ask, stream, interactive, session, health, benchmark
- Rich terminal formatting for better user experience
-
Wrapper Module (
wrapper.py)ResponseCache: SHA256-based caching with TTL supportClaudeWrapper: Adds retry logic with exponential backoff- Handles errors and timeouts gracefully
-
Session Module (
session.py)Session: Data class for conversation storageSessionManager: CRUD operations for sessions- Session templates for common use cases
- Export to JSON and Markdown formats
-
Approval Module (
approval.py)- Abstract
ApprovalStrategybase class - Eight concrete strategy implementations
- Factory function for strategy creation
- Predefined strategy presets
- Abstract
Data Flow
- User provides prompt and options 2.Claif options are converted to ClaudeCodeOptions
- Request is forwarded to claude_code_sdk
- Responses are streamed back from Claude API
- Optional caching based on prompt+options hash
- Messages saved to session if ID provided
- Formatted output returned to user
Development
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=src/claif_cla --cov-report=term-missing
Code Quality
# Format code
ruff format src/claif_cla tests
# Run linting
ruff check src/claif_cla tests
# Type checking
mypy src/claif_cla
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
License
MIT License - see LICENSE file for details.
Related Projects
- Claif - The main Claif framework
- claude-code-sdk - Anthropic's Claude SDK
- claif_cod -Claif provider for OpenAI Codex
- claif_gem -Claif provider for Google Gemini
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claif_cla-1.0.12.tar.gz.
File metadata
- Download URL: claif_cla-1.0.12.tar.gz
- Upload date:
- Size: 27.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1100c244fc3d997220b8b6ace6226f59fd4bf7de603288d2b5e9f6f4b34c606
|
|
| MD5 |
77e980a9a51dd1103504a32c44dc2c5d
|
|
| BLAKE2b-256 |
1cdd3fe9d29f8c340391e016dbb9875a15e464a69ef3d9c4ced9b678e735a6b0
|
File details
Details for the file claif_cla-1.0.12-py3-none-any.whl.
File metadata
- Download URL: claif_cla-1.0.12-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8aa9dc6ab0cc6855cec921c6369c6ce2c401e2de9540cb59d7c4b2adc6d42713
|
|
| MD5 |
3bb0c544d5bb2df4b0697e3c457007bb
|
|
| BLAKE2b-256 |
767100809340396e2ba6c31cfb8f75506412a4863d322da2ab2664ba9792da1b
|