Skip to content

Repository files navigation

Agent Optimizer

An API service for AI agent security, and prompt optimization.

Problem Statement

Organizations deploying AI agents face critical challenges in ensuring their systems are secure, compliant, and optimized:

  1. Prompt Optimization Complexity: Developing effective system prompts for AI agents is iterative and time-consuming. Teams lack automated tools to generate variations and optimize prompts against expected outputs.

  2. Security Blind Spots: System prompts are vulnerable to adversarial attacks such as prompt injection, jailbreaking, and prompt poisoning. Organizations need systematic testing to identify vulnerabilities before deployment.

  3. Lack of Automated Security Testing: Even when vulnerabilities are identified, executing comprehensive security tests against system prompts requires specialized knowledge and tooling.

This API addresses these challenges by providing a unified service for policy-to-guardrail mapping, prompt optimization, security advisory, and automated security testing.


Features

Feature Description
Agent Prompt Optimizer Generates dataset variations and optimizes prompts
AI Security Advisor Recommends security tests for system prompts
Security Tester Executes security tests against system prompts
Prompt Improver Hardens system prompts based on failed security tests

API Endpoints

The API is split into two servers:

  1. Main API Server (port 8000): Security testing and prompt hardening

    • /security-advisor
    • /security-test
    • /improve-prompt
  2. Opik Endpoints Server (port 8001): Dataset creation and prompt optimization

    • /agent-optimize
    • /job-status

1. Agent Optimize

Generates a dataset from seed data and runs prompt optimization in the background.

Endpoint: POST /agent-optimize

Request:

Content-Type: application/x-www-form-urlencoded

dataset_name=<string>
system_prompt=<string>
user_input=<string>
function_call=<string>
expected_output=<string>
llm_response=<string>          # optional
no_of_generations=<int>        # 1-9, default: 9

Response:

{
  "status": "started",
  "dataset_name": "<dataset_name>",
  "job_id": "<job_id>",
  "message": "Job started. Use GET /job-status with X-Job-ID=<job_id> header to check progress."
}

2. Job Status

Retrieves logs for a running or completed optimization job.

Endpoint: GET /job-status

Headers:

X-Job-ID: <job_id>

Response:

{
  "job_id": "<job_id>",
  "optimized_system_prompt": "<optimized_prompt_if_completed>",
  "log": "<log_content>"
}

3. Security Advisor

Analyzes a system prompt and recommends security tests to perform.

Endpoint: POST /security-advisor

Request:

Content-Type: application/x-www-form-urlencoded

system_prompt=<string>

Response:

{
  "recommended_tests": [
    {
      "test_id": "PROMPT_INJECTION",
      "priority": "critical",
      "rationale": "No explicit instruction boundary enforcement"
    },
    {
      "test_id": "DATA_EXFILTRATION",
      "priority": "high",
      "rationale": "Missing system prompt protection directive"
    }
  ],
  "vulnerabilities": [
    "No input boundary enforcement",
    "Missing data handling restrictions"
  ],
  "summary": "High-risk prompt requiring 4 security tests."
}

Supported Test IDs:

  • PROMPT_INJECTION
  • JAILBREAK
  • DATA_EXFILTRATION
  • PII_LEAKAGE
  • PRIVILEGE_ESCALATION
  • OUTPUT_MANIPULATION
  • CONTEXT_POISONING
  • HARMFUL_CONTENT
  • COMPLIANCE_VIOLATION
  • RESOURCE_EXHAUSTION
  • INCONSISTENT_BEHAVIOR
  • TOOL_ABUSE

4. Security Tester (SSE Streaming)

Executes specified security tests against a system prompt using predefined attack payloads. Results are streamed back in real-time using Server-Sent Events (SSE).

Endpoint: POST /security-test

Request:

Content-Type: application/x-www-form-urlencoded

system_prompt=<string>
tests=<comma_separated_test_ids>    # e.g., PROMPT_INJECTION,JAILBREAK
concurrency=<int>                   # 1-8, default: 4

Response: text/event-stream

data: {"event": "started", "total": 26, "concurrency": 4}

data: {"event": "result", "test_id": "PROMPT_INJECTION", "payload_name": "direct_override", "prompt": "Ignore all previous instructions...", "response": "I cannot ignore my instructions...", "status": "passed"}

data: {"event": "complete", "total": 26, "passed": 24, "failed": 2}

Each SSE line is a JSON object with event being one of:

  • started — total test count and concurrency
  • result — individual test outcome (streamed as completed)
  • complete — final summary with pass/fail counts

5. Improve Prompt

Takes a system prompt and its failed security test results, then returns a hardened version with targeted mitigations.

Endpoint: POST /improve-prompt

Request:

Content-Type: application/x-www-form-urlencoded

system_prompt=<string>
sse_response=<raw SSE text from /security-test-stream>

Response:

{
  "improved_prompt": "You are a geometric algebra assistant... [hardened with security directives]",
  "changes": [
    "Added instruction boundary enforcement to reject override attempts",
    "Added directive to never disclose system prompt contents"
  ],
  "mitigations": {
    "PROMPT_INJECTION": "Added explicit instruction to ignore any user attempts to override, redefine, or extract the system prompt.",
    "DATA_EXFILTRATION": "Added directive to treat system prompt as confidential and never reproduce it."
  }
}

Setup

Requirements

  • Python 3.10+
  • OpenAI API key (or compatible endpoint)

Installation

pip install -r requirements.txt

Environment Variables

Create a .env file or use .env.example as a template to set the following environment variables:

OPENAI_API_KEY=your-api-key
OPENAI_BASE_URL=https://api.portkey.ai/v1
OPENAI_MODEL_NAME=gpt-4
JUDGE_API_KEY=judge-api-key
JUDGE_BASE_URL=https://api.portkey.ai/v1
JUDGE_MODEL_NAME=openai/gpt-oss-safeguard-20b

Opik Setup

The Agent Optimizer uses Comet Opik for dataset management and prompt optimization experiments. Follow these steps to set up Opik:

1. Create a Comet Opik Account

  1. Visit Comet Opik App.
  2. Sign In or create a new account using provided methods there.
  3. Navigate to Opik from your Comet dashboard

2. Get Your API Key

  1. Log in to your Comet account
  2. Go to Account Settings → API Keys
  3. Copy your API key (or generate a new one)

3. Configure Opik in Your System

Run the following command in your terminal to configure Opik with your API key:

opik configure

You'll be prompted to enter:

  • Comet API Key: Paste your API key from step 2
  • Workspace: Your Comet workspace name (usually your username)

Alternatively, you can set the environment variable directly:

export OPIK_API_KEY=your-opik-api-key
export OPIK_WORKSPACE=your-workspace-name

Or add to your .env file:

OPIK_API_KEY=your-opik-api-key
OPIK_WORKSPACE=your-workspace-name

4. Verify Configuration

Test your Opik configuration:

opik whoami

This should display your configured workspace and confirm the connection.


Run

This project consists of two FastAPI applications:

1. Main API Server (main.py)

Provides security testing, policy extraction, and prompt hardening endpoints.

uvicorn main:app --host 0.0.0.0 --port 8000 --reload

The server starts at http://localhost:8000

API Documentation:

2. Opik Endpoints Server (opik-endpoints/main.py)

Provides dataset creation and prompt optimization with background job management.

cd opik-endpoints
uvicorn main:app --host 0.0.0.0 --port 8001 --reload

The server starts at http://localhost:8001

API Documentation:


Utilities

SSE to Table Converter

sse_to_table.py converts raw SSE data streams (from /security-test-stream) into readable markdown tables.

# Convert a standalone SSE response file
python sse_to_table.py response.json

# Process a markdown file, replacing inline SSE blocks with tables
python sse_to_table.py -m review.md -o review-formatted.md

Project Structure

Agent-Optimizer/
├── main.py                   # FastAPI application (Main API Server - Port 8000)
├── dataset_create.py         # Dataset generation module
├── optimizer.py              # Prompt optimization script
├── sse_to_table.py           # SSE-to-markdown-table converter
├── security-advisor.md       # Security advisor system prompt
├── security-tests.md         # Security tests catalog
├── requirements.txt          # Python dependencies
├── Case Study.md             # Detailed usage case study example
├── review.md                 # Iterative prompt hardening log (raw SSE)
├── review-formatted.md       # Iterative prompt hardening log (tables)
├── response.json             # Case Study's first test run's SSE response data
├── opik-endpoints/           # Opik API Server (Port 8001)
│   ├── main.py               # FastAPI application for Opik endpoints
│   ├── dataset_create.py     # Dataset creation logic
│   └── optimizer.py          # Optimization execution
└── security-testcases/       # Attack payload definitions
    ├── PROMPT_INJECTION.json
    ├── JAILBREAK.json
    ├── DATA_EXFILTRATION.json
    ├── PII_LEAKAGE.json
    ├── PRIVILEGE_ESCALATION.json
    ├── OUTPUT_MANIPULATION.json
    ├── CONTEXT_POISONING.json
    ├── HARMFUL_CONTENT.json
    ├── COMPLIANCE_VIOLATION.json
    ├── RESOURCE_EXHAUSTION.json
    ├── INCONSISTENT_BEHAVIOR.json
    └── TOOL_ABUSE.json

About

Lightweight toolkit for optimizing multi-agent behavior and training workflows

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages