Skip to content

ganesh44we/api-reliability-rl-agent-1

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

52 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

API Reliability RL Agent

A Decision Intelligence System for Smart API Optimization


Overview

This project implements a decision intelligence system that optimizes API usage under uncertain conditions using Reinforcement Learning (RL)-inspired logic.

Instead of blindly calling APIs, the system:

  • observes current conditions
  • selects an action
  • evaluates the outcome
  • improves decisions over time

Goal: maximize success, minimize latency, and reduce cost


Problem Statement

Real-world APIs are unreliable due to:

  • ❌ Failures
  • ⏱️ High latency
  • πŸ’Έ Cost constraints
  • ⚑ System load fluctuations

Traditional systems use fixed rules, which are inefficient.

This project builds a dynamic decision engine that adapts in real time.


Core Concept

The system follows a Reinforcement Learning loop:

State β†’ Action β†’ Reward β†’ Next State

πŸ“Œ State (Observation)

Each API call produces:

  • api_status β†’ success / failed
  • latency β†’ response time
  • retry_count β†’ retries attempted
  • api_cost β†’ cost incurred
  • system_load β†’ low / medium / high

🎯 Actions

The agent can choose:

Action Description
accept Accept current response
retry Retry API call
switch_api Switch to alternative API
use_cache Use cached response
return_error Stop and return failure

πŸ† Reward System

Each action is evaluated with a reward (0–1):

βœ”οΈ Positive factors

  • successful API call
  • low latency
  • minimal retries
  • low cost

❌ Penalties

  • excessive retries
  • high latency
  • unnecessary switching

🧠 Agent Behavior

The system learns decision patterns such as:

failed + low retries β†’ retry  
success + low latency β†’ accept  
too many retries β†’ switch_api  
high load β†’ use_cache  

πŸ‘‰ It balances performance, cost, and reliability.


πŸ—οΈ Architecture

Evaluator / User
        ↓
Inference Script (Agent)
        ↓
API Calls (/reset, /step)
        ↓
FastAPI Backend
        ↓
Environment Logic
        ↓
Reward + Next State

πŸ“‚ Project Structure

api-reliability-rl-agent/
β”‚
β”œβ”€β”€ inference.py            # Agent logic + evaluation loop
β”œβ”€β”€ models.py               # Typed models
β”œβ”€β”€ openenv.yaml            # OpenEnv configuration
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ uv.lock
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile
β”‚
β”œβ”€β”€ server/
β”‚   β”œβ”€β”€ app.py              # FastAPI backend
β”‚   └── environment.py      # RL environment logic
β”‚
└── app.py                  # (Optional) Gradio UI

πŸ”Œ API Endpoints

πŸ”Ή Health Check

GET /

Response:

{"status": "ok"}

πŸ”Ή Reset Environment

POST /reset

Request:

{
  "difficulty": "easy"
}

πŸ‘‰ Initializes new episode πŸ‘‰ Reward = 0 (no action yet)


πŸ”Ή Step (Execute Action)

POST /step

Request:

{
  "action": {
    "action": "retry"
  }
}

Response:

{
  "observation": {...},
  "reward": 0.83,
  "done": false
}

πŸ‘‰ Applies action and returns reward


⚠️ Important Clarification

πŸ‘‰ The API does NOT decide actions

/step = executor only

πŸ‘‰ YOU provide action β†’ system evaluates it


πŸ€– Viewing Agent Decisions (MOST IMPORTANT)

πŸ‘‰ The actual decision-making happens in:

python inference.py

Example Output

[START] task=easy env=openenv_api_env
[STEP] step=1 action=retry reward=0.85 done=false
[STEP] step=2 action=accept reward=0.92 done=false
[STEP] step=3 action=accept reward=0.91 done=false
[END] success=true steps=10 score=0.92

πŸ” What this means

  • action=... β†’ decision made by agent
  • reward=... β†’ quality of decision
  • score=... β†’ average performance

πŸ‘‰ This is what evaluators use.


πŸ§ͺ Testing

Swagger UI

πŸ‘‰ https://rahilahmed1945-api-reliability-rl-agent.hf.space/docs


Testing Flow

reset β†’ step β†’ step β†’ observe rewards

⚠️ In Swagger:

  • YOU choose actions manually
  • System only evaluates

🌐 Deployment

πŸ‘‰ Live App:

https://rahilahmed1945-api-reliability-rl-agent.hf.space


Ports

  • External (HF): 7860
  • Internal (FastAPI): 8000

🐳 Docker

Build

docker build -t api-agent .

Run

docker run -p 8000:8000 api-agent

πŸ“Š Performance

Difficulty Score
Easy ~0.90
Medium ~0.60
Hard ~0.50

🧠 Key Insights

  • Not just what action, but when matters
  • Excess retries reduce reward
  • Efficient decisions yield higher scores
  • System balances reliability vs cost

πŸš€ Future Improvements

  • πŸ€– LLM-based decision reasoning
  • πŸ“Š Visualization dashboard
  • 🧠 Deep RL (DQN, PPO)
  • 🌍 Real API integrations

🏁 Conclusion

This project demonstrates a scalable, explainable decision intelligence system for optimizing API reliability using RL principles.


About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 76.2%
  • Shell 22.9%
  • Dockerfile 0.9%