Skip to content

Latest commit

 

History

History

README.md

LangChain Customer Service Agent with MeshGuard Governance

A production-ready customer service AI agent built with LangChain and governed by MeshGuard. This example demonstrates tiered permissions, tool governance, and safe autonomous agent operations.

Architecture

┌─────────────────────────────────────────────────────────────────────────────┐
│                           Customer Service Agent                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│  ┌──────────────┐     ┌──────────────────────────────────────────────────┐  │
│  │   LangChain  │     │              MeshGuard Governance                │  │
│  │    Agent     │────▶│  ┌─────────┐  ┌──────────┐  ┌─────────────────┐  │  │
│  │  (GPT-4/etc) │     │  │  Basic  │  │ Elevated │  │      Admin      │  │  │
│  └──────────────┘     │  │  Tier   │  │   Tier   │  │      Tier       │  │  │
│                       │  │         │  │          │  │                 │  │  │
│                       │  │ • Read  │  │ • Refunds│  │ • Account       │  │  │
│                       │  │   Data  │  │   ≤ $50  │  │   Changes       │  │  │
│                       │  │ • View  │  │ • Send   │  │ • Override      │  │  │
│                       │  │  Orders │  │   Emails │  │   Policies      │  │  │
│                       │  └────┬────┘  └────┬─────┘  └────────┬────────┘  │  │
│                       └───────┼────────────┼─────────────────┼───────────┘  │
│                               │            │                 │              │
│  ┌────────────────────────────┼────────────┼─────────────────┼───────────┐  │
│  │                      Governed Tools                                   │  │
│  │  ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────────┐  │  │
│  │  │ Customer Tools  │ │  Order Tools    │ │   Refund Tools          │  │  │
│  │  │ • lookup        │ │ • lookup_order  │ │ • check_eligibility     │  │  │
│  │  │ • get_history   │ │ • get_status    │ │ • process_refund ⚠️     │  │  │
│  │  └─────────────────┘ └─────────────────┘ └─────────────────────────┘  │  │
│  │  ┌─────────────────────────────────────────────────────────────────┐  │  │
│  │  │                      Email Tools                                │  │  │
│  │  │                 • send_customer_email ⚠️                        │  │  │
│  │  └─────────────────────────────────────────────────────────────────┘  │  │
│  └───────────────────────────────────────────────────────────────────────┘  │
│                                                                              │
│  ┌───────────────────────────────────────────────────────────────────────┐  │
│  │                         Mock Services                                 │  │
│  │  ┌───────────────┐  ┌───────────────┐  ┌────────────────────────────┐ │  │
│  │  │   Mock CRM    │  │ Mock Orders   │  │     Mock Payments          │ │  │
│  │  │ (12 customers)│  │ (25+ orders)  │  │  (refund processing)       │ │  │
│  │  └───────────────┘  └───────────────┘  └────────────────────────────┘ │  │
│  └───────────────────────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────────────────────┘

Features

  • 🔒 Tiered Permissions: Basic, Elevated, and Admin access levels
  • 🛡️ Tool Governance: MeshGuard @governed_tool decorator on sensitive operations
  • 📧 Email Controls: Governed email sending with audit logging
  • 💰 Refund Limits: Automatic enforcement of $50 refund cap for elevated tier
  • 🧪 Mock Services: Realistic sample data for development and testing
  • 🐳 Docker Ready: Run everything with a single command

Quick Start

Prerequisites

  • Python 3.11+
  • OpenAI API key
  • MeshGuard SDK (or mock mode for development)

Installation

# Clone and enter the directory
cd langchain-customer-service

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Copy environment template
cp .env.example .env

# Edit .env with your API keys
nano .env

Running the Agent

# Interactive mode
python main.py

# With a specific query
python main.py --query "What's the status of order ORD-2024-001?"

# With elevated permissions
python main.py --tier elevated

# Docker
docker-compose up

Permission Tiers

Tier Capabilities Use Case
basic Read customer data, view orders, check order status Frontline support, information lookup
elevated All basic + refunds up to $50, send emails Senior support, simple resolutions
admin All elevated + unlimited refunds, account changes Supervisors, escalations

Governed Tools

Basic Tier Tools

  • lookup_customer(email_or_phone) - Find customer by email or phone
  • get_customer_history(customer_id) - View interaction history
  • lookup_order(order_id) - Get order details
  • get_order_status(order_id) - Check delivery status

Elevated Tier Tools (Governed)

  • check_refund_eligibility(order_id) - Check if order qualifies for refund
  • process_refund(order_id, amount, reason) - Process refund (max $50)
  • send_customer_email(customer_id, subject, body) - Send email to customer

Admin Tier Tools (Governed)

  • All elevated tools with no amount limits
  • update_customer_account(customer_id, changes) - Modify account details

Example Conversations

Basic Tier - Information Lookup

User: Can you look up the customer with email sarah.johnson@email.com?

Agent: I found the customer:
  - Name: Sarah Johnson
  - Customer ID: CUST-002
  - Email: sarah.johnson@email.com
  - Phone: +1-555-0102
  - Member since: 2022-03-15
  - Lifetime value: $1,247.50

Elevated Tier - Processing Refund

User: Customer CUST-005 wants a refund for order ORD-2024-008

Agent: Let me check the refund eligibility for that order.

The order ORD-2024-008 is eligible for a refund:
  - Order total: $34.99
  - Reason eligible: Within 30-day return window
  - Items: Wireless Mouse (qty: 1)

Shall I process the refund of $34.99?

User: Yes, please process it

Agent: ✅ Refund processed successfully!
  - Refund ID: REF-2024-0042
  - Amount: $34.99
  - Method: Original payment method
  - Expected: 3-5 business days

I'll send a confirmation email to the customer now.

Denied Action - Exceeding Limits

User: Process a $150 refund for order ORD-2024-012

Agent: ⚠️ I cannot process this refund. The amount ($150.00) exceeds my 
authorization limit of $50.00 for the elevated tier.

Options:
1. I can process a partial refund up to $50.00
2. I can escalate this to an admin for full refund approval
3. I can document this request for supervisor review

What would you like me to do?

Policy Configuration

The MeshGuard policy (policies/customer-service.yaml) defines:

tiers:
  basic:
    tools:
      - customer.lookup_customer
      - customer.get_customer_history
      - orders.lookup_order
      - orders.get_order_status
    
  elevated:
    inherits: basic
    tools:
      - refunds.check_eligibility
      - refunds.process_refund
      - email.send_customer_email
    constraints:
      refunds.process_refund:
        max_amount: 50.00
        
  admin:
    inherits: elevated
    constraints:
      refunds.process_refund:
        max_amount: null  # unlimited

Testing

# Run all tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=. --cov-report=html

# Test specific tier
pytest tests/test_permissions.py -k "elevated"

Docker Deployment

# Build and run
docker-compose up --build

# Run in background
docker-compose up -d

# View logs
docker-compose logs -f agent

# Stop
docker-compose down

Project Structure

langchain-customer-service/
├── README.md
├── requirements.txt
├── .env.example
├── main.py                 # Entry point
├── agent.py                # LangChain agent with governance
├── tools/
│   ├── __init__.py
│   ├── customer.py         # Customer lookup tools
│   ├── orders.py           # Order management tools
│   ├── refunds.py          # Refund processing (governed)
│   └── email.py            # Email sending (governed)
├── services/
│   ├── __init__.py
│   ├── mock_crm.py         # Mock CRM with sample data
│   ├── mock_orders.py      # Mock order database
│   └── mock_payments.py    # Mock payment processor
├── policies/
│   └── customer-service.yaml
├── tests/
│   ├── __init__.py
│   ├── test_tools.py
│   ├── test_permissions.py
│   └── test_agent.py
├── Dockerfile
└── docker-compose.yml

Environment Variables

Variable Required Description
OPENAI_API_KEY Yes OpenAI API key for LLM
MESHGUARD_API_KEY No* MeshGuard API key (*mock mode if not set)
MESHGUARD_POLICY_ID No Policy ID to use
PERMISSION_TIER No Default tier (basic/elevated/admin)
LOG_LEVEL No Logging level (DEBUG/INFO/WARN/ERROR)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

License

MIT License - See LICENSE file for details.