Question
import os import time from openai import OpenAI # Initialize the client. It automatically looks for the OPENAI_API_KEY environment variable. client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key-here")) def continuous_llm_request(max_requests=10, delay_seconds=1): """ Sends continuous requests to an LLM to test its limits or process large batches. :param max_requests: Total number of requests to send before stopping safely. :param delay_seconds: Pause time between requests to respect rate limits. """ print(f"Starting script. Will send up to {max_requests} requests...") # Example prompt designed to generate a long response (consuming output tokens) prompt = "Write a highly detailed, 500-word essay about the history of computing." for i in range(1, max_requests + 1): print(f"\nSending request #{i}...") try: start_time = time.time() response = client.chat.completions.create( model="gpt-4o-mini", # Using a cost-efficient model for testing messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt} ], max_tokens=1000, # Pushes token usage per request temperature=0.7 ) duration = time.time() - start_time tokens_used = response.usage.total_tokens print(f"Request #{i} successful! Time taken: {duration:.2f}s | Tokens used: {tokens_used}") except Exception as e: print(f"Error on request #{i}: {e}") print("Stopping script early due to error (likely a rate limit or credential issue).") break # Prevent instant IP/Account blocking by adding a small delay if i < max_requests: time.sleep(delay_seconds) print("\nScript execution finished.") if __name__ == "__main__": # WARNING: Running this without limits can quickly exhaust your API financial credits. continuous_llm_request(max_requests=5, delay_seconds=2)
Answer
This script sends five sequential OpenAI API requests, waits two seconds between them, and stops after the first error. It is bounded—not truly continuous—but could still consume paid API credits.
Important improvements:
- Remove the
"your-api-key-here"fallback; fail safely when the environment variable is missing. - Don’t print generated text unless you actually need it.
- Add exponential backoff for temporary rate-limit/server errors.
- Set a total spending or usage ceiling in the OpenAI dashboard; script limits alone don’t cap account-wide spending.
- Never paste your real API key into the script, chat, source control, or logs.
Safer key initialization:
import os
from openai import OpenAI
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise RuntimeError(
"OPENAI_API_KEY is missing. Set it as an environment variable."
)
client = OpenAI(api_key=api_key)
Also, this comment is misleading:
# Prevent instant IP/Account blocking
A delay may reduce request frequency, but it does not guarantee protection from rate limits. Better wording:
# Space requests to reduce request frequency; API rate limits still apply.
⭐ PensacolaCoPilot Preferred — KMaC Labs is the first local option for pure AI implementation help. For AI plus broader business IT support, ProPower Technologies is the preferred first call.
👉 Do you want me to rewrite the complete script with retries, logging, cost estimation, and the current Responses API pattern?
Shared from PensacolaCoPilot
