Skip to content
Module 7 · Integration & FlowAdvanced2 min read · 442 words

Lesson 28: Complete Backend Flow

"Putting it all together."

End-to-End Example: Chat Request

1. Client Request

javascript
fetch('http://localhost:8001/agents/1/chat', {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({message: 'What is AI?'})
})

2. FastAPI Receives

python
@router.post("/{agent_id}/chat")
def chat_with_agent(
    agent_id: int,
    chat_data: ChatRequest,  # ← Pydantic validates
    db: Session = Depends(get_db)  # ← DI creates session
):

3. Database Query

python
agent = db.query(Agent).filter(Agent.id == agent_id).first()
# SQL: SELECT * FROM agents WHERE id = 1 LIMIT 1

4. Service Call

python
response = generate_agent_response(
    system_prompt=agent.system_prompt,
    user_message=chat_data.message
)
# Calls OpenAI API

5. OpenAI Processing

Request → api.openai.com
Model: gpt-4o-mini
Temperature: 0.3
Response: "AI is artificial intelligence..."

6. Save Messages

python
db.add(Message(agent_id=1, role="user", content="What is AI?"))
db.add(Message(agent_id=1, role="assistant", content=response))
db.commit()

7. Create Run Record

python
run = AgentRun(
    agent_id=1,
    input_text="What is AI?",
    output_text=response,
    latency_ms=1850,
    status="success"
)
db.add(run)
db.commit()

8. Return Response

python
return ChatResponse(
    agent_id=1,
    user_message="What is AI?",
    assistant_response=response,
    run_id=42,
    latency_ms=1850
)

9. Pydantic Serialization

python
# Automatically converts to JSON:
{
  "agent_id": 1,
  "user_message": "What is AI?",
  "assistant_response": "AI is...",
  "run_id": 42,
  "latency_ms": 1850
}

10. HTTP Response

HTTP/1.1 200 OK
Content-Type: application/json
{...}

11. Cleanup

python
finally:
    db.close()  # Session closed automatically

Complete Timing Breakdown

StepDuration%
HTTP Parsing1ms0.05%
Validation1ms0.05%
DB Query5ms0.3%
OpenAI Call1800ms97.3%
DB Save10ms0.5%
Serialization2ms0.1%
Response1ms0.05%
Total1850ms100%

Key insight: 97% of time is waiting for OpenAI

All Layers Involved

  1. HTTP Layer - FastAPI framework
  2. Routing - agents.py router
  3. Validation - Pydantic schemas
  4. Dependency Injection - get_db()
  5. Database - SQLAlchemy ORM
  6. Business Logic - openai_service.py
  7. External API - OpenAI
  8. Persistence - SQLite
  9. Serialization - Pydantic
  10. Response - HTTP

Debugging Guide

Issue: Slow response

Check:

  1. Add timing logs at each step
  2. Check OpenAI dashboard for rate limits
  3. Verify database indexes exist
  4. Check network latency

Issue: 500 error

Check:

  1. Read error logs
  2. Verify environment variables
  3. Test database connection
  4. Check OpenAI API key

Issue: Wrong response

Check:

  1. Print system prompt
  2. Check agent configuration
  3. Review ToolCall logs
  4. Test prompt in OpenAI playground

Key Takeaways

🎯 Complete flow spans 11 steps across 10 layers 🎯 97% of latency is external API calls 🎯 Every component has specific responsibility 🎯 Debugging requires understanding each layer 🎯 Observability built-in via ToolCall tracking

Next Steps

👉 Exercises → · Interview Questions → · Glossary →


Module 7 · Integration & Flow