This guide shows how to work with paginated API responses when listing prompts. Pagination allows you to efficiently retrieve large numbers of prompts without overwhelming your application or the API.
import osfrom baytos.claro import BaytClientclient = BaytClient(api_key=os.getenv("BAYT_API_KEY"))# Get first pageresult = client.list_prompts(limit=10)print(f"Page 1: {len(result['prompts'])} prompts")# Get second page using cursorif result['hasMore']: result = client.list_prompts(limit=10, cursor=result['cursor']) print(f"Page 2: {len(result['prompts'])} prompts")
import osfrom baytos.claro import BaytClientclient = BaytClient(api_key=os.getenv("BAYT_API_KEY"))all_prompts = []cursor = Nonewhile True: # Fetch a page result = client.list_prompts(limit=20, cursor=cursor) # Add prompts to our list all_prompts.extend(result['prompts']) print(f"Fetched {len(result['prompts'])} prompts (total: {len(all_prompts)})") # Check if there are more pages if not result['hasMore']: break # Update cursor for next page cursor = result['cursor']print(f"\nTotal prompts: {len(all_prompts)}")
import osfrom baytos.claro import BaytClientdef get_prompts_limited(client, max_prompts=50): """Fetch up to max_prompts prompts""" all_prompts = [] cursor = None while len(all_prompts) < max_prompts: # Calculate how many more we need remaining = max_prompts - len(all_prompts) limit = min(remaining, 50) # API max is typically 50-100 # Fetch a page result = client.list_prompts(limit=limit, cursor=cursor) # Add prompts all_prompts.extend(result['prompts']) # Check if we should continue if not result['hasMore']: break # Update cursor cursor = result['cursor'] return all_prompts[:max_prompts]client = BaytClient(api_key=os.getenv("BAYT_API_KEY"))prompts = get_prompts_limited(client, max_prompts=25)print(f"Fetched {len(prompts)} prompts")
Process prompts as you fetch them (memory efficient):
Copy
import osfrom baytos.claro import BaytClientclient = BaytClient(api_key=os.getenv("BAYT_API_KEY"))def process_prompt(prompt): """Process a single prompt""" print(f"Processing: {prompt.title}") # Do something with the prompt return { 'title': prompt.title, 'package': prompt.package_name, 'has_context': prompt.has_context() }# Process prompts page by pageprocessed = []cursor = Nonepage = 1while True: print(f"\nFetching page {page}...") result = client.list_prompts(limit=20, cursor=cursor) # Process each prompt for prompt in result['prompts']: processed.append(process_prompt(prompt)) # Check if done if not result['hasMore']: break cursor = result['cursor'] page += 1print(f"\nProcessed {len(processed)} prompts total")
import osfrom baytos.claro import BaytClientdef iterate_all_prompts(client, page_size=50): """Generator that yields all prompts""" cursor = None while True: result = client.list_prompts(limit=page_size, cursor=cursor) # Yield each prompt for prompt in result['prompts']: yield prompt # Check if done if not result['hasMore']: break cursor = result['cursor']# Usageclient = BaytClient(api_key=os.getenv("BAYT_API_KEY"))for prompt in iterate_all_prompts(client): print(f"- {prompt.title}")
Generators are memory-efficient because they only load one page at a time, making them ideal for processing large numbers of prompts.
import osfrom baytos.claro import BaytClientclient = BaytClient(api_key=os.getenv("BAYT_API_KEY"))# Find all prompts with context filesprompts_with_files = []cursor = Nonewhile True: result = client.list_prompts(limit=50, cursor=cursor) # Filter prompts with files for prompt in result['prompts']: if prompt.has_context(): files = prompt.get_file_contexts() if files: prompts_with_files.append(prompt) print(f"Found: {prompt.title} ({len(files)} files)") if not result['hasMore']: break cursor = result['cursor']print(f"\nTotal prompts with files: {len(prompts_with_files)}")
import osimport jsonfrom baytos.claro import BaytClientdef save_cursor(cursor, filename='cursor.json'): """Save cursor to file""" with open(filename, 'w') as f: json.dump({'cursor': cursor}, f)def load_cursor(filename='cursor.json'): """Load cursor from file""" try: with open(filename, 'r') as f: data = json.load(f) return data.get('cursor') except FileNotFoundError: return Noneclient = BaytClient(api_key=os.getenv("BAYT_API_KEY"))# Load previous cursor if existscursor = load_cursor()if cursor: print(f"Resuming from saved cursor...")else: print("Starting from beginning...")# Fetch next pageresult = client.list_prompts(limit=50, cursor=cursor)print(f"Fetched {len(result['prompts'])} prompts")# Save cursor for next runif result['hasMore']: save_cursor(result['cursor']) print("Cursor saved for next run")else: print("All prompts fetched")