InsideLLM Python SDK
A comprehensive Python SDK for LLM/Agent analytics that provides asynchronous event ingestion with LangChain integration and custom agent support.
Features
- Asynchronous Event Processing: Non-blocking event queuing with configurable batch processing
- LangChain Integration: Automatic tracking for LangChain agents, tools, and LLM calls
- Custom Agent Support: Decorators and context managers for easy integration with any agent framework
- Comprehensive Event Types: Support for 15+ essential event types covering the full LLM/Agent lifecycle
- Robust Error Handling: Retry mechanisms with exponential backoff and graceful failure handling
- Performance Monitoring: Built-in metrics and queue statistics
- Thread-Safe Operations: Designed for concurrent usage in multi-threaded environments
Installation
From PyPI
pip install insidellm
With LangChain support
pip install insidellm[langchain]
Development installation
git clone https://github.com/insidellm/python-sdk.git
cd python-sdk
pip install -e .[dev]
Quick Start
Basic Usage
import insidellm
# Initialize the SDK
insidellm.initialize(api_key="your-api-key")
# Start a session
client = insidellm.get_client()
run_id = client.start_run(user_id="user-123")
# Log events
user_event = insidellm.Event.create_user_input(
run_id=run_id,
user_id="user-123",
input_text="Hello, AI assistant!"
)
client.log_event(user_event)
# Events are automatically batched and sent asynchronously
# End the session
client.end_run(run_id)
insidellm.shutdown()
LangChain Integration
import insidellm
from langchain.llms import OpenAI
from langchain.agents import initialize_agent
# Initialize InsideLLM
insidellm.initialize(api_key="your-api-key")
# Create LangChain callback
callback = insidellm.InsideLLMCallback(
client=insidellm.get_client(),
user_id="user-123"
)
# Use with any LangChain component
llm = OpenAI(callbacks=[callback])
agent = initialize_agent(tools, llm, callbacks=[callback])
# All LLM calls, tool usage, and agent actions are automatically tracked
response = agent.run("What's the weather like today?")
Custom Agent Integration
Using Decorators
import insidellm
@insidellm.track_llm_call("gpt-4", "openai")
def call_llm(prompt):
# Your LLM call logic
return llm_response
@insidellm.track_tool_use("web_search", "api")
def search_web(query):
# Your tool logic
return search_results
@insidellm.track_agent_step("planning")
def plan_task(task):
# Your planning logic
return plan
Using Context Managers
import insidellm
with insidellm.InsideLLMTracker(user_id="user-123") as tracker:
# Log user input
input_id = tracker.log_user_input("Hello, how can you help?")
# Track LLM calls
with tracker.track_llm_call("gpt-4", "openai", "User greeting") as log_response:
response = call_llm("User greeting")
log_response(response)
# Track tool usage
with tracker.track_tool_call("calculator", {"expression": "2+2"}) as log_response:
result = calculator("2+2")
log_response(result)
# Log agent response
tracker.log_agent_response("Hello! I can help with many tasks.", parent_event_id=input_id)
Event Types
The SDK supports 15 comprehensive event types:
User Interaction
user_input- User inputs and queriesuser_feedback- User feedback and ratings
Agent Processing
agent_reasoning- Agent reasoning stepsagent_planning- Agent planning processesagent_response- Agent responses
LLM Operations
llm_request- LLM API requestsllm_response- LLM API responsesllm_streaming_chunk- Streaming response chunks
Tool/Function Calls
tool_call- Tool invocationstool_response- Tool resultsfunction_execution- Function executions
External APIs
api_request- External API callsapi_response- External API responses
Error Handling
error- General errorsvalidation_error- Validation failurestimeout_error- Timeout events
System Events
session_start- Session initiationsession_end- Session completionperformance_metric- Performance measurements
Configuration
Environment Variables
export INSIDELLM_API_KEY="your-api-key"
export INSIDELLM_BATCH_SIZE=50
export INSIDELLM_AUTO_FLUSH_INTERVAL=30.0
export INSIDELLM_MAX_RETRIES=3
Programmatic Configuration
import insidellm
config = insidellm.InsideLLMConfig(
max_queue_size=10000,
batch_size=50,
auto_flush_interval=30.0,
request_timeout=30.0,
max_retries=3,
raise_on_error=False
)
insidellm.initialize(api_key="your-api-key", config=config)
Advanced Usage
Manual Event Creation
import insidellm
from insidellm import Event, EventType
# Create custom events
event = Event(
run_id="your-run-id",
user_id="user-123",
event_type=EventType.PERFORMANCE_METRIC,
payload={
"metric_name": "response_time",
"metric_value": 250,
"metric_unit": "ms"
}
)
client = insidellm.get_client()
client.log_event(event)
Error Handling
import insidellm
try:
# Your agent logic
result = process_user_request()
except Exception as e:
# Log errors automatically
error_event = insidellm.Event.create_error(
run_id=current_run_id,
user_id=current_user_id,
error_type="processing_error",
error_message=str(e),
error_code=type(e).__name__
)
client.log_event(error_event)
Performance Monitoring
import insidellm
client = insidellm.get_client()
# Get queue statistics
stats = client.queue_manager.get_statistics()
print(f"Events queued: {stats['events_queued']}")
print(f"Success rate: {stats['success_rate']:.1f}%")
# Check client health
if client.is_healthy():
print("Client is healthy")
Examples
The repository includes comprehensive examples:
examples/basic_usage.py- Basic SDK functionalityexamples/langchain_example.py- LangChain integrationexamples/custom_agent_example.py- Custom agent integration
API Reference
Core Classes
InsideLLMClient- Main client for event loggingEvent- Event data modelInsideLLMTracker- Context manager for workflow trackingInsideLLMCallback- LangChain callback handler
Decorators
@track_llm_call()- Automatic LLM call tracking@track_tool_use()- Automatic tool usage tracking@track_agent_step()- Automatic agent step tracking
Configuration
InsideLLMConfig- Configuration management- Environment variable support for all settings
Requirements
- Python 3.8+
pydantic >= 2.0.0requests >= 2.25.0langchain >= 0.1.0(optional, for LangChain integration)
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run the test suite
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- Documentation: https://docs.insidellm.com/python-sdk
- Issues: https://github.com/insidellm/python-sdk/issues
- Email: support@insidellm.com
Metadata
Release files for insidellm 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| insidellm-1.0.0.tar.gz | 46.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| insidellm-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 78.3 kB
Release files / insidellm-1.0.0.tar.gz
| Download URL | insidellm-1.0.0.tar.gz |
|---|---|
| Size | 46.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
360634410b23def5f189233359eaf62f7cf1b01e9675f86424f1387786480bc3
|
|
BLAKE2b-256 checksum How to use checksums |
e256179fd093280e3715f878c4e4ec4e502465b33a0828e5d4ddc7bf6789ca0f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|
Release files / insidellm-1.0.0-py3-none-any.whl
| Download URL | insidellm-1.0.0-py3-none-any.whl |
|---|---|
| Size | 31.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7db353e7db201d411ad621f62235ac0b756414936de4398243bace2c8ddae4b4
|
|
BLAKE2b-256 checksum How to use checksums |
0933a4afedd6314eb8652cc707f5f1377d81264e7a5146d18f0db10ff91b5ee9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|