Skip to content

susom/claude_proxy

Repository files navigation

Claude Code API Proxy for REDCap SecureChatAI

A lightweight Python proxy that enables Claude Code to work with REDCap's SecureChatAI External Module by translating between Claude's API format and REDCap's API format.

Features

Zero-buffering SSE streaming - Instant response delivery
100% vanilla Python - No dependencies, just standard library
Docker support - Easy deployment with Docker Compose
Model mapping - Automatically maps Claude model names to SecureChatAI aliases
Full Claude API compatibility - Works seamlessly with Claude Code

Quick Start

1. Clone the Repository

git clone git@github.com:susom/claude_proxy.git
cd claude-code-proxy

2. Configure Environment Variables

cp .env.example .env

Edit .env with your REDCap credentials:

REDCAP_API_URL=http://your-redcap-server.com/api/
REDCAP_API_TOKEN=your_redcap_api_token_here
LISTEN_HOST=localhost
LISTEN_PORT=8765

3. Run with Docker Compose

docker compose up -d

The proxy will be available at http://localhost:8765.

4. Configure Claude Code

Set these environment variables in your shell:

export ANTHROPIC_AUTH_TOKEN="your_redcap_api_token_here"
export ANTHROPIC_BASE_URL="http://localhost:8765"

Then launch Claude Code and start chatting!

How It Works

Claude Code → Proxy (localhost:8765) → REDCap SecureChatAI → AI Models

The proxy:

  1. Receives Claude API requests from Claude Code
  2. Translates them to REDCap External Module API format
  3. Forwards to your REDCap SecureChatAI instance
  4. Streams responses back to Claude Code via Server-Sent Events (SSE)

Model Mapping

The proxy automatically maps Claude model names to SecureChatAI model aliases:

Claude Model SecureChatAI Alias
claude-haiku-4-5-20251001 gpt-4o
claude-3-7-sonnet-20250219 claude
claude-sonnet-4 claude
o3-mini o3-mini
o1 o1
gpt-4.1 gpt-4.1
deepseek deepseek

You can customize these mappings in claude_api_proxy.py.

Running Without Docker

If you prefer to run the proxy directly:

# Set environment variables
export REDCAP_API_URL="http://your-redcap-server.com/api/"
export REDCAP_API_TOKEN="your_token"
export LISTEN_HOST="localhost"
export LISTEN_PORT="8765"

# Run the proxy
python3 claude_api_proxy.py

Troubleshooting

Claude Code shows no response

  1. Check proxy logs: docker compose logs -f
  2. Verify environment variables are set correctly
  3. Ensure REDCap API URL is accessible from the proxy
  4. Restart Claude Code after changing proxy settings

Responses are slow or delayed

  • The proxy uses zero-buffering SSE streaming for instant delivery
  • Delays are likely from the upstream AI model, not the proxy
  • Check REDCap SecureChatAI logs for performance issues

Authentication errors

  • Verify REDCAP_API_TOKEN matches your REDCap token
  • Ensure the token has access to the SecureChatAI module
  • Check that ANTHROPIC_AUTH_TOKEN in Claude Code matches the REDCap token

Architecture

Key Components

  • UnbufferedHTTPRequestHandler: Custom HTTP handler with wbufsize=0 to disable buffering
  • SSE Streaming: Implements Anthropic's 6-event SSE sequence:
    1. message_start
    2. content_block_start
    3. content_block_delta (text content)
    4. content_block_stop
    5. message_delta (usage stats)
    6. message_stop

Anti-Buffering Techniques

  • wbufsize = 0 on request handler
  • wfile.flush() after every SSE event
  • Transfer-Encoding: chunked header
  • X-Accel-Buffering: no header (for nginx)
  • Socket-level TCP_CORK disabled

Security Notes

⚠️ This proxy is designed for local/internal use only

  • No additional authentication beyond token validation
  • Runs on localhost by default
  • Use VPN or firewall rules if exposing to network
  • Keep your .env file secure and never commit it to git

Development

Project Structure

claude-code-proxy/
├── claude_api_proxy.py    # Main proxy server
├── Dockerfile             # Docker image definition
├── docker-compose.yml     # Docker Compose configuration
├── .env.example          # Example environment variables
├── .gitignore            # Git ignore rules
└── README.md             # This file

Making Changes

  1. Edit claude_api_proxy.py
  2. Rebuild Docker image: docker compose build
  3. Restart: docker compose up -d

About

Claude Code API Proxy for REDCap SecureChatAI

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published