In today’s AI-driven world, deploying intelligent agents often comes with privacy concerns and recurring cloud costs. What if you could run powerful AI models entirely on your own machine, ensuring full control and customization—without any cloud fees? This guide walks you through setting up DeepSeek AI agents locally using Ollama and LangChain, enabling you to automate workflows, enhance productivity, and explore AI capabilities right from your laptop.

Why Run DeepSeek AI Agents Locally?
DeepSeek R1 is a robust open-source language model with 671B parameters, but its distilled versions (ranging from 1.5B to 70B) allow efficient local execution. Key benefits of running AI agents on your own system include:
✅ Privacy: Process sensitive data offline, free from third-party access.
✅ Cost Savings: Avoid expensive cloud API fees.
✅ Customization: Tailor agents for specific tasks such as customer support, content moderation, or data analysis.
For this guide, we’ll create a corporate crisis management team featuring two AI agents:
- Agent 1: Drafts press statements.
- Agent 2: Reviews them for legal risks.
Prerequisites
Hardware:
- 8GB+ RAM (for 7B-8B parameter models).
- M1 Mac or modern Intel/AMD CPU (GPU optional but not required).
Software:
- Ollama (for local model hosting).
- Python 3.8+ and Jupyter Notebook (for scripting).
Step 1: Install Ollama & Download DeepSeek R1
Download Ollama
Visit Ollama’s website, install the app, and open your terminal.
Pull a Distilled Model
ollama run deepseek-ai/deepseek-r1-8b
Smaller models like deepseek-r1-1.5b
work for basic tasks but sacrifice nuance.
Test the Model
>>> "What’s the capital of France?"
Paris.
Step 2: Set Up Python Environment
Create a Virtual Environment
python -m venv deepseek-env
source deepseek-env/bin/activate # Linux/Mac
deepseek-env\Scripts\activate # Windows
Install Libraries
pip install langchain langchain-community ipykernel
Step 3: Build AI Agents with LangChain
Objective:
Create a workflow where:
- Agent 1: Drafts a crisis response.
- Agent 2: Reviews it for legal risks.
Launch Jupyter Notebook
jupyter notebook
Import Libraries
from langchain.chains import LLMChain, SequentialChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import Ollama
Initialize DeepSeek R1
llm = Ollama(model="deepseek-r1-8b", temperature=0.7)
temperature=0.7
balances creativity and focus.
Define Prompts
Crisis Manager Prompt
crisis_template = """
As a Crisis Management Expert, draft a response to: {issue}.
Acknowledge the problem, reassure stakeholders, and outline steps to resolve it.
"""
crisis_prompt = PromptTemplate(
input_variables=["issue"],
template=crisis_template
)
Legal Reviewer Prompt
legal_template = """
As a Legal Expert, revise this statement: {crisis_response}.
Remove admissions of fault, ambiguous language, or potential liabilities.
Explain changes made.
"""
legal_prompt = PromptTemplate(
input_variables=["crisis_response"],
template=legal_template
)
Create Chains
crisis_chain = LLMChain(llm=llm, prompt=crisis_prompt, output_key="crisis_response")
legal_chain = LLMChain(llm=llm, prompt=legal_prompt, output_key="legal_response")
workflow = SequentialChain(
chains=[crisis_chain, legal_chain],
input_variables=["issue"],
output_variables=["crisis_response", "legal_response"]
)
Step 4: Execute the Workflow
Define a Crisis Scenario
issue = "Product Recall: 10,000 electric scooters due to battery fire risks."
Run the Chains
result = workflow({"issue": issue})
print("Initial Statement:\n", result["crisis_response"])
print("\nLegally Reviewed Statement:\n", result["legal_response"])
Sample Output:
Initial Statement:
"We regret to inform customers of a battery defect in Model X scooters. We’re issuing refunds..."
Legally Reviewed Statement:
"Our company is addressing concerns about Model X scooters. We’re offering inspections... [Changes: Removed ‘defect’ and ‘regret’ to avoid liability.]"
Customization & Advanced Tips
🔹 Swap Models: Replace deepseek-r1-8b
with llama3-70b
or mistral
in Ollama.
🔹 Add More Agents: Include a PR agent for tone optimization or a QA agent for fact-checking.
🔹 Speed Up Inference: Use smaller models (1.5B
) or quantized versions (deepseek-r1-8b-q4
).
Troubleshooting
🚨 Ollama Connection Issues: Ensure the app is running (ollama serve
).
🚨 Memory Errors: Reduce model size or close background apps.
🚨 Syntax Errors: Check for typos in PromptTemplate
variables.