Add AI-native capabilities to your Python applications with reflexive.chat().
This Python SDK brings the same powerful AI integration from the Node.js version to Python, allowing you to embed Claude-powered chat directly into your code.
# Install the Python SDK
pip install reflexive
# Install the Reflexive CLI (Node.js)
npm install -g reflexiveThe Python SDK requires the Node.js CLI for AI capabilities. The CLI provides the Claude Agent SDK integration, web dashboard, and debugger.
import reflexive
# Create Reflexive instance
r = reflexive.make_reflexive({'spawn_cli': True})
# Track state (visible to AI)
r.set_state('users.count', 42)
r.set_state('cache.hit_rate', 0.95)
# Ask AI questions
answer = r.chat('What is the cache hit rate?')
print(answer) # "The cache hit rate is currently 95%."
# AI has full context about your app
analysis = r.chat('Should I scale up based on user count?')
print(analysis)Run your Python app through the Reflexive CLI:
reflexive --debug app.pyYour Python code:
import reflexive
# Automatically connects to parent CLI
r = reflexive.make_reflexive()
# Chat works immediately!
response = r.chat('Analyze my app')Benefits:
- ✅ Full AI capabilities
- ✅ Web dashboard
- ✅ Debugger support
- ✅ No background processes
The SDK spawns the CLI automatically:
import reflexive
# Spawn CLI in background
r = reflexive.make_reflexive({
'spawn_cli': True, # Spawns npx reflexive
'debug': True,
'port': 3099
})
# Chat works!
response = r.chat('Hello AI')Benefits:
- ✅ No manual CLI command
- ✅ Works with standard
python app.py - ✅ Good for development
Create a Reflexive instance.
Options:
spawn_cli(bool): Spawn CLI in background (default: False)debug(bool): Enable debugger for spawned CLI (default: False)shell(bool): Enable shell access (default: False)write(bool): Enable file writing (default: True)port(int): Dashboard port (default: 3099)max_logs(int): Maximum log entries (default: 500)
Returns: ReflexiveInstance
Send a message to the AI and get a response.
answer = r.chat('What is the current memory usage?')The AI has full access to:
- Your app's state (set via
set_state()) - Logs
- Process metrics
- All MCP tools (file ops, shell, debugging)
Set state that's visible to the AI.
r.set_state('users.active', 42)
r.set_state('db.connections', 10)
r.set_state('feature_flags.new_ui', True)Use dot notation for hierarchy. Keep values small (metrics, not data).
Get state value(s).
count = r.get_state('users.active') # 42
all_state = r.get_state() # {'users.active': 42, ...}Add a log entry.
r.log('info', 'Processing started')
r.log('warn', 'High memory usage detected')
r.log('error', f'Failed to connect: {error}')Note: print() statements are automatically captured!
Retrieve log entries.
recent = r.get_logs(10) # Last 10 logs
errors = r.get_logs(50, 'error') # Last 50 errors
all_logs = r.get_logs() # All logs (up to max_logs)import reflexive
from http.server import HTTPServer, BaseHTTPRequestHandler
r = reflexive.make_reflexive({'spawn_cli': True})
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith('/story/'):
topic = self.path[7:]
# Use AI to generate content!
story = r.chat(f'Write a short story about: {topic}')
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write(story.encode())
# Track metrics
r.set_state('stories.generated', r.get_state('stories.generated') or 0 + 1)
HTTPServer(('', 8080), Handler).serve_forever()Visit http://localhost:8080/story/space-adventure
import reflexive
import time
r = reflexive.make_reflexive({'spawn_cli': True, 'debug': True})
records_processed = 0
errors = 0
for record_id in range(100):
success = process_record(record_id)
if success:
records_processed += 1
else:
errors += 1
# Update state
r.set_state('records.processed', records_processed)
r.set_state('records.errors', errors)
r.set_state('records.error_rate', errors / (record_id + 1))
# Ask AI for insights every 20 records
if record_id % 20 == 0:
analysis = r.chat('Analyze the pipeline health. Should I be concerned?')
print(f"AI: {analysis}")import reflexive
r = reflexive.make_reflexive({'spawn_cli': True, 'debug': True})
def complex_calculation(data):
result = 0
for item in data:
result += item * 2
# Ask AI to analyze
explanation = r.chat(f'Explain what this calculation does. Result: {result}')
print(explanation)
return result┌─────────────────────────────────────┐
│ Your Python App │
│ │
│ import reflexive │
│ r = make_reflexive( │
│ {'spawn_cli': True} │
│ ) │
│ r.chat('message') │
└──────────┬──────────────────────────┘
│ HTTP (localhost)
▼
┌─────────────────────────────────────┐
│ Reflexive CLI (Node.js) │
│ - Claude Agent SDK │
│ - Web Dashboard │
│ - MCP Tools │
│ - Debugger │
└─────────────────────────────────────┘
- Python SDK spawns
npx reflexiveas subprocess - CLI starts HTTP server on localhost
- SDK communicates via HTTP POST (SSE for streaming)
- AI responses flow back to Python
When run via reflexive app.py:
# Detects REFLEXIVE_CLI_MODE environment variable
r = reflexive.make_reflexive() # No spawn_cli needed!The SDK connects to the parent CLI automatically.
| Feature | Node.js | Python |
|---|---|---|
make_reflexive() |
✅ | ✅ |
.chat(message) |
✅ | ✅ |
.setState(key, value) |
✅ | ✅ .set_state() |
.getState(key) |
✅ | ✅ .get_state() |
.log(type, msg) |
✅ | ✅ |
| Auto log interception | ✅ | ✅ |
| Web dashboard | ✅ | ✅ (via CLI) |
| Custom tools | ✅ | ⏳ Coming soon |
| Async API | ✅ Native | ⏳ Coming soon |
Keep state small and informative:
# Good: Metrics and counters
r.set_state('requests.count', 1234)
r.set_state('cache.hit_rate', 0.95)
r.set_state('errors.last_hour', 5)
# Bad: Large objects
r.set_state('all_users', huge_list) # ❌ Don't do this!Be specific with your questions:
# Vague
r.chat('How are things?')
# Specific
r.chat('Is the error rate trending up?')
r.chat('Should I scale based on request count?')
r.chat('Explain this calculation: ...')Development:
r = reflexive.make_reflexive({
'spawn_cli': True,
'debug': True,
'shell': True
})Production (via CLI):
reflexive --write app.py# Minimal in prod
r = reflexive.make_reflexive()You need to either:
- Run with
reflexive app.py, OR - Set
spawn_cli: Truein options
Install the Node.js CLI:
npm install -g reflexiveCheck the CLI output. Common issues:
- Missing Node.js
- Python file not found
- CLI flags conflict
- API Docs: API.md
- Design: DESIGN.md
- Examples: examples/
- Node.js SDK: ../README.md
MIT