LangChain & CrewAI
Multi-agent AI frameworks
LangChain & CrewAI Integration with HiveOps
Build powerful AI agents and workflows using LangChain or CrewAI with HiveOps as your LLM backend. Get 98% cost savings vs OpenAI.
Overview
HiveOps is fully compatible with:
- LangChain - Chain multiple LLM calls, connect to data sources
- CrewAI - Build teams of specialized AI agents
- LlamaIndex - RAG (Retrieval-Augmented Generation) systems
- Semantic Kernel - Microsoft's AI orchestration framework
All frameworks work seamlessly with HiveOps by simply changing the base URL.
LangChain Integration
Quick Start (Python)
from langchain_openai import ChatOpenAI
# Just add openai_api_base - everything else works identically
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
# Use like normal
response = llm.invoke("What is quantum computing?")
print(response.content)
Environment Variables Setup
# .env
OPENAI_API_KEY=sk-YOUR-HIVEOPS-KEY
OPENAI_API_BASE=https://ai.hiveops.io
from langchain_openai import ChatOpenAI
# Automatically reads from environment
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_base="https://ai.hiveops.io"
)
LangChain Examples
Simple Chain
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
chain = prompt | llm | StrOutputParser()
result = chain.invoke({"topic": "programming"})
print(result)
RAG (Retrieval-Augmented Generation)
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
# Initialize LLM with HiveOps
llm = ChatOpenAI(
model="llama-3-70b-instruct", # Better for complex reasoning
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io",
temperature=0
)
# Load and split documents
loader = TextLoader("docs.txt")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
texts = text_splitter.split_documents(documents)
# Create embeddings (use OpenAI for embeddings, HiveOps for LLM)
embeddings = OpenAIEmbeddings(openai_api_key="sk-OPENAI-KEY")
vectorstore = Chroma.from_documents(texts, embeddings)
# Create RAG chain
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever()
)
# Query your documents
question = "What are the main features of the product?"
answer = qa_chain.run(question)
print(answer)
Conversational Agent
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.memory import ConversationBufferMemory
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
# Define tools
def calculate(expression):
"""Evaluate a mathematical expression"""
try:
return str(eval(expression))
except:
return "Error: Invalid expression"
def search_web(query):
"""Search the web (mock implementation)"""
return f"Results for: {query}"
tools = [
Tool(
name="Calculator",
func=calculate,
description="Useful for math calculations. Input: math expression like '2+2' or '15*3'"
),
Tool(
name="Search",
func=search_web,
description="Search the web for information"
)
]
# Initialize agent with memory
memory = ConversationBufferMemory(memory_key="chat_history")
agent = initialize_agent(
tools,
llm,
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
memory=memory,
verbose=True
)
# Use the agent
response = agent.run("What's 25 * 4?")
print(response)
response = agent.run("Search for latest AI news")
print(response)
Streaming with LangChain
from langchain_openai import ChatOpenAI
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io",
streaming=True,
callbacks=[StreamingStdOutCallbackHandler()]
)
# Streams to stdout
response = llm.invoke("Write a short story about a robot")
LangChain TypeScript/JavaScript
Setup
npm install langchain @langchain/openai
Basic Usage
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
modelName: "llama3:8b-instruct-q8_0",
openAIApiKey: "sk-YOUR-HIVEOPS-KEY",
configuration: {
baseURL: "https://ai.hiveops.io",
},
});
const response = await llm.invoke("What is TypeScript?");
console.log(response.content);
Chain Example
import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
const llm = new ChatOpenAI({
modelName: "llama3:8b-instruct-q8_0",
openAIApiKey: process.env.HIVEOPS_API_KEY!,
configuration: {
baseURL: "https://ai.hiveops.io",
},
});
const prompt = PromptTemplate.fromTemplate(
"Translate the following to {language}: {text}",
);
const chain = prompt.pipe(llm).pipe(new StringOutputParser());
const result = await chain.invoke({
language: "Spanish",
text: "Hello, how are you?",
});
console.log(result);
CrewAI Integration
CrewAI uses LangChain under the hood, so setup is identical.
Install CrewAI
pip install crewai crewai-tools
Simple Crew Example
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
# Configure LLM
llm = ChatOpenAI(
model="llama-3-70b-instruct", # Use larger model for agent reasoning
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io",
temperature=0.7
)
# Define agents
researcher = Agent(
role="Research Analyst",
goal="Gather comprehensive information on given topics",
backstory="You are an expert researcher with years of experience in data analysis.",
llm=llm,
verbose=True
)
writer = Agent(
role="Content Writer",
goal="Create engaging, well-structured articles",
backstory="You are a professional writer skilled at turning research into compelling content.",
llm=llm,
verbose=True
)
# Define tasks
research_task = Task(
description="Research the benefits and challenges of remote work in 2024.",
agent=researcher,
expected_output="A detailed list of benefits and challenges with supporting data"
)
writing_task = Task(
description="Write a 500-word blog post about remote work based on the research.",
agent=writer,
expected_output="A polished blog post ready for publication"
)
# Create crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
verbose=True
)
# Execute
result = crew.kickoff()
print(result)
Multi-Agent Research Crew
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="llama-3-70b-instruct",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
# Define specialized agents
data_analyst = Agent(
role="Data Analyst",
goal="Analyze data and extract insights",
backstory="Expert in statistical analysis and data interpretation.",
llm=llm
)
market_researcher = Agent(
role="Market Researcher",
goal="Research market trends and competitor analysis",
backstory="Specialized in market intelligence and competitive analysis.",
llm=llm
)
strategist = Agent(
role="Business Strategist",
goal="Develop actionable business strategies",
backstory="Senior strategist with experience in business planning.",
llm=llm
)
report_writer = Agent(
role="Report Writer",
goal="Create professional, executive-ready reports",
backstory="Expert in business communication and report writing.",
llm=llm
)
# Define sequential tasks
task1 = Task(
description="Analyze the current state of the AI industry, focusing on trends and growth areas.",
agent=data_analyst,
expected_output="Statistical analysis with key insights"
)
task2 = Task(
description="Research top 5 competitors in the AI tools market and their strategies.",
agent=market_researcher,
expected_output="Competitive analysis report"
)
task3 = Task(
description="Based on the analysis and research, develop a go-to-market strategy for a new AI product.",
agent=strategist,
expected_output="Strategic recommendations"
)
task4 = Task(
description="Compile all findings into a comprehensive executive report.",
agent=report_writer,
expected_output="Professional executive report"
)
# Create sequential crew
crew = Crew(
agents=[data_analyst, market_researcher, strategist, report_writer],
tasks=[task1, task2, task3, task4],
process=Process.sequential, # Tasks run in order
verbose=True
)
result = crew.kickoff()
print(result)
CrewAI with Tools
from crewai import Agent, Task, Crew
from crewai_tools import SerperDevTool, WebsiteSearchTool
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="llama-3-70b-instruct",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
# Initialize tools
search_tool = SerperDevTool()
web_tool = WebsiteSearchTool()
# Agent with tools
researcher = Agent(
role="AI Research Specialist",
goal="Find the latest developments in AI",
backstory="You are an expert at finding and analyzing cutting-edge AI research.",
llm=llm,
tools=[search_tool, web_tool], # Agent can use these tools
verbose=True
)
task = Task(
description="Research the latest breakthroughs in large language models from the past month.",
agent=researcher,
expected_output="Summary of top 5 recent LLM breakthroughs"
)
crew = Crew(
agents=[researcher],
tasks=[task],
verbose=True
)
result = crew.kickoff()
print(result)
LlamaIndex Integration
Basic Setup
from llama_index.llms.openai import OpenAI
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# Configure LLM
llm = OpenAI(
model="llama3:8b-instruct-q8_0",
api_key="sk-YOUR-HIVEOPS-KEY",
api_base="https://ai.hiveops.io"
)
# Load documents
documents = SimpleDirectoryReader("data").load_data()
# Create index
index = VectorStoreIndex.from_documents(documents, llm=llm)
# Query
query_engine = index.as_query_engine()
response = query_engine.query("What is the main topic of these documents?")
print(response)
Cost Comparison: OpenAI vs HiveOps
LangChain Agent Example Cost
Scenario: 1000 agent runs/day, avg 2000 tokens per run
| Provider | Model | Cost/Day | Cost/Month |
|---|---|---|---|
| OpenAI | GPT-3.5 | $30 | $900 |
| OpenAI | GPT-4 | $600 | $18,000 |
| HiveOps | Llama 3 8B | $0.60 | $18 |
| HiveOps | Llama 3 70B | $6 | $180 |
Savings: 98% with Llama 3 8B, 99% with 70B (vs GPT-4)
Calculate Your CrewAI Costs
def estimate_crew_cost(
num_agents,
avg_tokens_per_agent,
runs_per_day,
model="llama-3-70b-instruct"
):
# Model pricing (input + output average)
pricing = {
"llama3:8b-instruct-q8_0": 0.015,
"llama-3-70b-instruct": 0.15,
"gemma-2-9b-it": 0.0075
}
cost_per_million = pricing.get(model, 0.015)
total_tokens_per_run = num_agents * avg_tokens_per_agent
total_tokens_per_day = total_tokens_per_run * runs_per_day
daily_cost = (total_tokens_per_day / 1_000_000) * cost_per_million
monthly_cost = daily_cost * 30
print(f"CrewAI Configuration:")
print(f" Agents: {num_agents}")
print(f" Tokens/agent: {avg_tokens_per_agent}")
print(f" Runs/day: {runs_per_day}")
print(f" Model: {model}\n")
print(f"Total tokens/day: {total_tokens_per_day:,}")
print(f"Daily cost: ${daily_cost:.4f}")
print(f"Monthly cost: ${monthly_cost:.2f}")
# Example: 4-agent crew, 3000 tokens each, 100 runs/day
estimate_crew_cost(
num_agents=4,
avg_tokens_per_agent=3000,
runs_per_day=100,
model="llama-3-70b-instruct"
)
Best Practices
1. Choose the Right Model
| Use Case | Recommended Model | Why |
|---|---|---|
| Simple agents | llama3:8b-instruct-q8_0 | Fast, cost-effective |
| Complex reasoning | llama-3-70b-instruct | Better for multi-step tasks |
| High-volume tasks | gemma-2-9b-it | Balance of cost/quality |
2. Optimize Token Usage
from langchain_openai import ChatOpenAI
# For quick agent tasks
llm_fast = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io",
max_tokens=500 # Limit response length
)
# For complex reasoning
llm_complex = ChatOpenAI(
model="llama-3-70b-instruct",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io",
max_tokens=2000
)
# Use appropriate model based on task complexity
simple_agent = Agent(llm=llm_fast, ...)
complex_agent = Agent(llm=llm_complex, ...)
3. Enable Caching (LangChain)
from langchain.cache import InMemoryCache
from langchain.globals import set_llm_cache
# Cache LLM responses to avoid redundant API calls
set_llm_cache(InMemoryCache())
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
# First call hits API
llm.invoke("What is 2+2?")
# Second call uses cache (no API cost)
llm.invoke("What is 2+2?")
4. Monitor Token Usage
from langchain.callbacks import get_openai_callback
with get_openai_callback() as cb:
result = agent.run("Analyze the market trends")
print(f"Tokens used: {cb.total_tokens}")
print(f"Prompt tokens: {cb.prompt_tokens}")
print(f"Completion tokens: {cb.completion_tokens}")
# Calculate HiveOps cost (Llama 3 8B)
cost = (
cb.prompt_tokens / 1_000_000 * 0.01 +
cb.completion_tokens / 1_000_000 * 0.02
)
print(f"Estimated cost: ${cost:.6f}")
Real-World Examples
Automated Content Pipeline (CrewAI)
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="llama-3-70b-instruct",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io"
)
# Content pipeline agents
topic_generator = Agent(
role="Topic Researcher",
goal="Find trending topics in the tech industry",
llm=llm
)
outliner = Agent(
role="Content Strategist",
goal="Create detailed article outlines",
llm=llm
)
writer = Agent(
role="Content Writer",
goal="Write engaging 1000-word articles",
llm=llm
)
editor = Agent(
role="Editor",
goal="Polish and optimize content",
llm=llm
)
# Sequential pipeline
tasks = [
Task(description="Find 3 trending AI topics", agent=topic_generator),
Task(description="Create outlines for each topic", agent=outliner),
Task(description="Write full articles from outlines", agent=writer),
Task(description="Edit and finalize articles", agent=editor)
]
crew = Crew(
agents=[topic_generator, outliner, writer, editor],
tasks=tasks,
process=Process.sequential
)
result = crew.kickoff()
print(result)
RAG-Powered Q&A Bot (LangChain)
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
# Use HiveOps for LLM
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_key="sk-YOUR-HIVEOPS-KEY",
openai_api_base="https://ai.hiveops.io",
temperature=0
)
# Use OpenAI for embeddings (HiveOps doesn't support embeddings yet)
embeddings = OpenAIEmbeddings(openai_api_key="sk-OPENAI-EMBEDDING-KEY")
# Load your knowledge base
vectorstore = FAISS.load_local("knowledge_base", embeddings)
# Create conversational chain
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
qa_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=vectorstore.as_retriever(),
memory=memory
)
# Use the Q&A bot
response = qa_chain({"question": "What are your product features?"})
print(response["answer"])
response = qa_chain({"question": "How much does it cost?"})
print(response["answer"])
Troubleshooting
Common Issues
Issue: "Model not found" error
# Wrong
llm = ChatOpenAI(model="gpt-3.5-turbo", ...)
# Correct
llm = ChatOpenAI(model="llama3:8b-instruct-q8_0", ...)
Issue: Base URL not set
# Must include base_url
llm = ChatOpenAI(
model="llama3:8b-instruct-q8_0",
openai_api_base="https://ai.hiveops.io" # Don't forget!
)
Issue: Using old LangChain version
# Update to latest
pip install --upgrade langchain langchain-openai
Next Steps
- API Reference - Full endpoint documentation
- Chatbots Guide - Build conversational agents
- Error Handling - Production tips
- LangChain Docs
- CrewAI Docs
Support
- 💬 Discord Community
- 📧 Email: [email protected]
- 📖 Code Examples