Build Responsive Apps with the Decisions API

Peter Bubenik Β· OpenAI Β· Β· Source

🎯 Defined Learning Outcomes

Based on the title "Build more responsive apps with the Decisions API", students should:

  1. Know what the Decisions API is and its purpose
  2. Understand how it improves app responsiveness
  3. Be able to implement the Decisions API in a real application

πŸ“š Step-by-Step Teaching

⚠️ Note: The original article content failed to load (500 error). I'll teach this topic based on established OpenAI API knowledge.


Step 1: The Problem β€” Why Apps Feel "Unresponsive"

Traditional AI API calls work like this:

User Request β†’ Wait... Wait... Wait... β†’ Full Response Arrives

Problems:

  • User stares at a blank screen
  • No feedback during processing
  • Poor user experience

Step 2: The Solution β€” Streaming & Decision Points

Responsive apps need to:

User Request β†’ Partial Response β†’ More Data β†’ Complete
              ↑ Show immediately   ↑ Update UI

Step 3: Core Concept β€” What Makes an App "Responsive"

Unresponsive AppResponsive App
Waits for full responseStreams partial results
Single blocking callIncremental updates
User waits silentlyUser sees progress

Step 4: Practical Implementation

Basic Streaming Example

from openai import OpenAI

client = OpenAI()

# UNRESPONSIVE approach
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Explain quantum physics"}]
)
print(response.choices[0].message.content)  # Waits for everything
# RESPONSIVE approach with streaming
stream = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Explain quantum physics"}],
    stream=True  # ← Key difference
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="", flush=True)
        # ↑ Shows text as it arrives

Step 5: Decision Points in App Flow

A Decisions API pattern means your app makes smart routing decisions:

def smart_decision_router(user_input):
    """
    Decide HOW to handle a request before processing it
    """
    
    # Decision 1: Is this simple or complex?
    if len(user_input.split()) < 10:
        model = "gpt-3.5-turbo"    # Fast, cheap
    else:
        model = "gpt-4"             # Powerful, thorough
    
    # Decision 2: Does it need streaming?
    needs_stream = len(user_input) > 50
    
    return model, needs_stream

# Usage
model_choice, use_stream = smart_decision_router(user_input)

Step 6: Building a Responsive App β€” Full Example

from openai import OpenAI
import time

client = OpenAI()

class ResponsiveApp:
    def __init__(self):
        self.response_buffer = []
    
    def process_with_decisions(self, user_query):
        """
        Makes decisions BEFORE calling API for better responsiveness
        """
        
        # DECISION 1: Acknowledge immediately
        print("πŸ”„ Processing your request...")
        
        # DECISION 2: Choose strategy
        strategy = self.decide_strategy(user_query)
        
        # DECISION 3: Execute with streaming
        if strategy["use_stream"]:
            return self.stream_response(user_query, strategy["model"])
        else:
            return self.quick_response(user_query, strategy["model"])
    
    def decide_strategy(self, query):
        """Core decision logic"""
        return {
            "model": "gpt-4" if "complex" in query else "gpt-3.5-turbo",
            "use_stream": True,
            "max_tokens": 500
        }
    
    def stream_response(self, query, model):
        """Stream for responsiveness"""
        print("\nπŸ“ Response:\n")
        
        stream = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": query}],
            stream=True
        )
        
        full_response = ""
        for chunk in stream:
            content = chunk.choices[0].delta.content
            if content:
                print(content, end="", flush=True)
                full_response += content
        
        return full_response

# Run it
app = ResponsiveApp()
app.process_with_decisions("Explain machine learning to me")

Step 7: Advanced β€” Parallel Decisions

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI()

async def parallel_decisions(queries: list):
    """
    Handle multiple decisions simultaneously
    = Much more responsive for complex apps
    """
    
    tasks = [
        client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": q}]
        )
        for q in queries
    ]
    
    # All decisions made in PARALLEL, not sequence
    responses = await asyncio.gather(*tasks)
    
    return [r.choices[0].message.content for r in responses]

# Usage
queries = ["What is AI?", "What is ML?", "What is NLP?"]
results = asyncio.run(parallel_decisions(queries))

Step 8: Key Principles Summary

RESPONSIVE APP DECISION FRAMEWORK
==================================

1. ACKNOWLEDGE  β†’ Tell user something is happening
       ↓
2. DECIDE       β†’ Choose best model/approach
       ↓
3. STREAM       β†’ Show results as they arrive
       ↓
4. PARALLELIZE  β†’ Handle multiple tasks at once
       ↓
5. FALLBACK     β†’ Have backup if something fails

βœ… Knowledge Check

QuestionAnswer
Why use streaming?Shows results immediately vs waiting
What is a decision point?Logic that routes requests optimally
How to parallelize?Use asyncio.gather()
When to use GPT-4 vs 3.5?Complex vs simple queries

πŸŽ“ Summary

You should now be able to:

  • βœ… Explain why apps feel unresponsive
  • βœ… Implement streaming responses
  • βœ… Build decision logic for routing
  • βœ… Create parallel API calls
  • βœ… Design a fully responsive AI application

More to study