Install
Install AbstractCore with your preferred providers
# Install everything
pip install abstractcore[all]
# Or install specific providers
pip install abstractcore[openai]
pip install abstractcore[anthropic]
Configure
Set up your API keys or local providers
# For cloud providers
export OPENAI_API_KEY="your-key-here"
export ANTHROPIC_API_KEY="your-key-here"
# For local providers (no keys needed)
# Install Ollama, LMStudio, or MLX
Code
Start building with the unified interface
from abstractcore import create_llm
# Create LLM instance
llm = create_llm("openai", model="gpt-4o-mini")
# Generate response
response = llm.generate("Hello, world!")
print(response.content)
# Check current configuration
abstractcore --status
# Set global fallback model (used when no app-specific default)
abstractcore --set-global-default ollama/llama3:8b
# Set app-specific defaults for optimal performance
abstractcore --set-app-default summarizer openai gpt-4o-mini
abstractcore --set-app-default cli ollama qwen3:4b
# Configure vision fallback for text-only models
abstractcore --download-vision-model # Download local model
# OR use existing model:
# abstractcore --set-vision-caption qwen2.5vl:7b
# Set API keys
abstractcore --set-api-key openai sk-your-key-here
# Configure logging
abstractcore --set-console-log-level WARNING
# Now use without specifying provider/model every time!
python -m abstractcore.utils.cli --prompt "Hello!" # Uses configured defaults
from abstractcore import create_llm
# Works with any provider - same API everywhere
llm = create_llm("openai", model="gpt-4o")
# Attach any file type with media parameter
response = llm.generate(
"What's in this image and document?",
media=["photo.jpg", "report.pdf"]
)
# Or use CLI with @filename syntax
# python -m abstractcore.utils.cli --prompt "Analyze @report.pdf"
# Supported file types:
# - Images: PNG, JPEG, GIF, WEBP, BMP, TIFF
# - Documents: PDF, DOCX, XLSX, PPTX
# - Data: CSV, TSV, TXT, MD, JSON
# Same code works with any provider
llm = create_llm("anthropic", model="claude-3.5-sonnet")
response = llm.generate(
"Summarize these materials",
media=["chart.png", "data.csv", "presentation.pptx"]
)
from abstractcore import create_llm
# Vision works across all providers with same interface
openai_llm = create_llm("openai", model="gpt-4o")
response = openai_llm.generate(
"Describe this image in detail",
media=["photo.jpg"]
)
# Same code with local provider
ollama_llm = create_llm("ollama", model="qwen2.5vl:7b")
response = ollama_llm.generate(
"What objects do you see?",
media=["scene.jpg"]
)
# Vision fallback for text-only models
# Configure once: abstractcore --download-vision-model
text_llm = create_llm("lmstudio", model="qwen/qwen3-next-80b") # No native vision
response = text_llm.generate(
"What's in this image?",
media=["complex_scene.jpg"]
)
# Works transparently: vision model analyzes → text model processes description
# Multi-image analysis
response = llm.generate(
"Compare these architectural styles",
media=["building1.jpg", "building2.jpg", "building3.jpg"]
)
from abstractcore import create_llm
from abstractcore.tools import tool
@tool
def calculate(expression: str) -> float:
"""Perform mathematical calculation."""
return eval(expression)
# Custom tag configuration for agent CLI compatibility
llm = create_llm(
"ollama",
model="qwen3-coder:30b",
tool_call_tags="function_call" # Converts to ...
)
# Real-time streaming with format conversion
for chunk in llm.generate("Calculate 15 * 23", tools=[calculate], stream=True):
if chunk.tool_calls:
print(f"Tool detected: {chunk.tool_calls[0].name}")
print(chunk.content, end="", flush=True)
from abstractcore import BasicSession, create_llm
llm = create_llm("openai", model="gpt-4o-mini")
session = BasicSession(llm, system_prompt="You are a helpful assistant.")
response1 = session.generate("My name is Alice")
response2 = session.generate("What's my name?") # Remembers context
# Auto-compaction with SOTA 2025 algorithm
session.compact(target_tokens=8000) # Compresses while preserving context
# Advanced analytics
summary = session.generate_summary(style="executive", length="brief")
assessment = session.generate_assessment(criteria=["clarity", "helpfulness"])
facts = session.extract_facts(entity_types=["person", "organization", "date"])
# Save with analytics
session.save("conversation.json", summary=True, assessment=True, facts=True)
from abstractcore import create_llm
from abstractcore.utils.token_utils import estimate_tokens
# Unified token parameters work across ALL providers
llm = create_llm(
"anthropic",
model="claude-haiku-4.5",
max_tokens=32000, # Context window (input + output)
max_output_tokens=8000, # Maximum output tokens
max_input_tokens=24000 # Maximum input tokens (auto-calculated if not set)
)
# Token estimation and validation
text = "Your input text here..."
estimated = estimate_tokens(text, model="claude-haiku-4.5")
print(f"Estimated tokens: {estimated}")
# Budget validation with warnings
response = llm.generate("Write a detailed analysis...")
print(f"Input tokens: {response.usage.input_tokens}")
print(f"Output tokens: {response.usage.output_tokens}")
print(f"Cost estimate: ${response.usage.cost_usd:.4f}")
# Start OpenAI-compatible server
# uvicorn abstractcore.server.app:app --host 0.0.0.0 --port 8000
import requests
# NEW: OpenAI Responses API with native file support
response = requests.post(
"http://localhost:8000/v1/responses",
json={
"model": "gpt-4o",
"input": [
{
"role": "user",
"content": [
{"type": "input_text", "text": "Analyze this document"},
{"type": "input_file", "file_url": "https://example.com/report.pdf"}
]
}
],
"stream": False # Optional streaming
}
)
# Or use standard chat completions with @filename syntax
import openai
client = openai.OpenAI(base_url="http://localhost:8000/v1", api_key="unused")
response = client.chat.completions.create(
model="ollama/qwen3-coder:30b",
messages=[{"role": "user", "content": "Analyze @report.pdf"}]
)