A Decision Intelligence System for Smart API Optimization
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
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.
The system follows a Reinforcement Learning loop:
State β Action β Reward β Next State
Each API call produces:
api_statusβ success / failedlatencyβ response timeretry_countβ retries attemptedapi_costβ cost incurredsystem_loadβ low / medium / high
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 |
Each action is evaluated with a reward (0β1):
- successful API call
- low latency
- minimal retries
- low cost
- excessive retries
- high latency
- unnecessary switching
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.
Evaluator / User
β
Inference Script (Agent)
β
API Calls (/reset, /step)
β
FastAPI Backend
β
Environment Logic
β
Reward + Next State
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
GET /Response:
{"status": "ok"}POST /resetRequest:
{
"difficulty": "easy"
}π Initializes new episode π Reward = 0 (no action yet)
POST /stepRequest:
{
"action": {
"action": "retry"
}
}Response:
{
"observation": {...},
"reward": 0.83,
"done": false
}π Applies action and returns reward
π The API does NOT decide actions
/step = executor only
π YOU provide action β system evaluates it
π The actual decision-making happens in:
python inference.py[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
action=...β decision made by agentreward=...β quality of decisionscore=...β average performance
π This is what evaluators use.
π https://rahilahmed1945-api-reliability-rl-agent.hf.space/docs
reset β step β step β observe rewards
- YOU choose actions manually
- System only evaluates
π Live App:
https://rahilahmed1945-api-reliability-rl-agent.hf.space
- External (HF): 7860
- Internal (FastAPI): 8000
docker build -t api-agent .docker run -p 8000:8000 api-agent| Difficulty | Score |
|---|---|
| Easy | ~0.90 |
| Medium | ~0.60 |
| Hard | ~0.50 |
- Not just what action, but when matters
- Excess retries reduce reward
- Efficient decisions yield higher scores
- System balances reliability vs cost
- π€ LLM-based decision reasoning
- π Visualization dashboard
- π§ Deep RL (DQN, PPO)
- π Real API integrations
This project demonstrates a scalable, explainable decision intelligence system for optimizing API reliability using RL principles.