Edgee’s OpenAI-compatible API works seamlessly with LangChain, allowing you to leverage LangChain’s powerful features like chains, agents, memory, and RAG while reducing token costs by up to 50% through intelligent compression and maintaining control over your LLM infrastructure.
Installation
Using uv with inline dependencies (PEP 723):
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "langchain",
# "langchain-openai",
# ]
# ///
Or install manually:
pip install langchain langchain-openai
Basic Usage
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
import os
# Initialize the LLM with Edgee endpoint
llm = ChatOpenAI(
base_url="https://api.edgee.ai/v1",
api_key=os.getenv("API_KEY"),
model="mistral-small", # or any model available through Edgee
timeout=30.0,
)
# Simple chat
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="What is LangChain?"),
]
response = llm.invoke(messages)
print(response.content)
Command-Line Script
Complete script with argument parsing:
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "langchain",
# "langchain-openai",
# ]
# ///
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
import os
import argparse
def main():
parser = argparse.ArgumentParser(description="LangChain with Edgee")
parser.add_argument("--model", type=str, default="mistral-small", help="Model name")
parser.add_argument("--message", type=str, required=True, help="Message to send")
parser.add_argument("--system", type=str, default="You are a helpful assistant.", help="System prompt")
args = parser.parse_args()
llm = ChatOpenAI(
base_url="https://api.edgee.ai/v1",
api_key=os.getenv("API_KEY"),
model=args.model,
timeout=30.0,
)
messages = [
SystemMessage(content=args.system),
HumanMessage(content=args.message),
]
response = llm.invoke(messages)
print(response.content)
if __name__ == "__main__":
main()
Usage Examples
# Basic usage (uses mistral-small by default)
uv run langchain_script.py --message "Tell me a joke"
# With custom model
uv run langchain_script.py --model "gpt-4" --message "Explain quantum computing"
# With custom system prompt
uv run langchain_script.py \
--model "mistral-small" \
--message "Write a haiku" \
--system "You are a creative poet"
Control Agentic Token Compression and add tags for observability using the default_headers parameter:
from langchain_openai import ChatOpenAI
import os
llm = ChatOpenAI(
base_url="https://api.edgee.ai/v1",
api_key=os.getenv("API_KEY"),
model="gpt-5.2",
default_headers={
"x-edgee-enable-compression": "true",
"x-edgee-compression-rate": "0.8", # Target 80% compression
"x-edgee-tags": "production,langchain,rag-pipeline",
}
)
Available Headers:
| Header | Type | Description |
|---|
x-edgee-enable-compression | "true" or "false" | Enable token compression (overrides console settings) |
x-edgee-compression-rate | string | Target compression rate (0.0-1.0, default 0.75) |
x-edgee-tags | string | Comma-separated tags for analytics and filtering |
You can also enable compression per API key in the Edgee console. Headers override console settings.
Advanced Features
Chains
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(
base_url="https://api.edgee.ai/v1",
api_key=os.getenv("API_KEY"),
model="mistral-small",
)
# Create a prompt template
prompt = PromptTemplate.from_template("Write a brief summary about {topic}")
# Create the chain using LCEL (LangChain Expression Language)
chain = prompt | llm | StrOutputParser()
# Run the chain
result = chain.invoke({"topic": "artificial intelligence"})
print(result)
Streaming Responses
llm = ChatOpenAI(
base_url="https://api.edgee.ai/v1",
api_key=os.getenv("API_KEY"),
model="mistral-small",
streaming=True,
)
for chunk in llm.stream("Tell me a long story"):
print(chunk.content, end="", flush=True)
You can add tags to your requests for analytics and filtering using the default_headers parameter:
from langchain_openai import ChatOpenAI
import os
llm = ChatOpenAI(
base_url="https://api.edgee.ai/v1",
api_key=os.getenv("API_KEY"),
model="mistral-small",
default_headers={
"x-edgee-tags": "production,user-123,langchain",
},
)
# All requests from this client will include these tags
response = llm.invoke("What is LangChain?")
Tags are comma-separated strings in the header. They help you categorize and filter requests in Edgee’s analytics dashboard.
Authentication
Edgee uses standard Bearer token authentication. Set your API key as an environment variable:
export API_KEY="sk-edgee-..."
The api_key parameter in ChatOpenAI automatically formats the header as:
Authorization: Bearer {api_key}