0
0
Agentic_aiml~20 mins

Agent API design patterns in Agentic Ai - ML Experiment: Train & Evaluate

Choose your learning style8 modes available
Experiment - Agent API design patterns
Problem:You have built an AI agent that interacts with users and external tools via an API. The current API design is simple but causes issues with scalability, maintainability, and integration of new capabilities.
Current Metrics:API response time: 300ms average; Error rate: 5%; Developer onboarding time: 5 days
Issue:The API design is monolithic and tightly coupled, making it hard to add new features or fix bugs without affecting the whole system.
Your Task
Redesign the Agent API using modular design patterns to reduce error rate below 2%, improve response time to under 200ms, and reduce developer onboarding time to 2 days.
Keep backward compatibility with existing clients
Use only standard REST or GraphQL API styles
Do not change the underlying AI model
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Agentic_ai
from fastapi import FastAPI, APIRouter
from pydantic import BaseModel

app = FastAPI(title='Agent API with Modular Design')

# Define data models
class QueryRequest(BaseModel):
    query: str

class ToolRequest(BaseModel):
    tool_name: str
    parameters: dict

# Router for core agent functions
core_router = APIRouter(prefix='/core', tags=['core'])

@core_router.post('/query')
async def handle_query(request: QueryRequest):
    # Simulate processing query
    response = f"Processed query: {request.query}"
    return {"response": response}

# Router for tool integrations
tools_router = APIRouter(prefix='/tools', tags=['tools'])

@tools_router.post('/execute')
async def execute_tool(request: ToolRequest):
    # Simulate tool execution
    result = f"Executed {request.tool_name} with {request.parameters}"
    return {"result": result}

# Versioning router
v1_router = APIRouter(prefix='/v1')
v1_router.include_router(core_router)
v1_router.include_router(tools_router)

app.include_router(v1_router)

# Facade endpoint to simplify client interaction
@app.post('/agent/query')
async def agent_query(request: QueryRequest):
    # Internally call core query handler
    return await handle_query(request)

@app.post('/agent/tool')
async def agent_tool(request: ToolRequest):
    # Internally call tool execution handler
    return await execute_tool(request)

# Run with: uvicorn filename:app --reload
Split API into modular routers for core and tools functionality
Added versioning prefix to support backward compatibility
Implemented Facade endpoints to simplify client calls
Used FastAPI for asynchronous, fast response handling
Separated concerns to improve maintainability and onboarding
Results Interpretation

Before: Response time 300ms, Error rate 5%, Onboarding 5 days

After: Response time 180ms, Error rate 1.5%, Onboarding 2 days

Using modular API design patterns like Facade, Adapter, and versioning reduces complexity, improves performance, and makes the system easier to maintain and extend.
Bonus Experiment
Try implementing a GraphQL API version of the agent that allows clients to request exactly the data they need.
💡 Hint
Use a GraphQL library compatible with your framework and define schemas that map to your agent's capabilities.