book-openCommon Patterns

Production-ready patterns for conversation lifecycle, user enrichment, webhooks, and routing.

Production-ready patterns for building with the Amigo AI API. Each pattern includes Python and TypeScript SDK examples.

1. Conversation Lifecycle Management

Create a conversation, send messages, handle streaming events, and finish cleanly.

spinner
from amigo_sdk import AsyncAmigoClient
from amigo_sdk.models import (
    ConversationCreateConversationRequest,
    CreateConversationParametersQuery,
    InteractWithConversationParametersQuery,
    Format,
)

async def run_conversation():
    async with AsyncAmigoClient() as client:
        # 1. Create conversation
        params = CreateConversationParametersQuery(
            service_id="your-service-id",
            response_format="text",
        )
        body = ConversationCreateConversationRequest()

        conversation_id = None
        async for event in await client.conversations.create_conversation(body, params):
            if hasattr(event, "conversation_id"):
                conversation_id = event.conversation_id
            # Handle greeting events...

        # 2. Interact
        interact_params = InteractWithConversationParametersQuery(
            request_format=Format.text,
            response_format="text",
        )
        async for event in await client.conversations.interact_with_conversation(
            conversation_id,
            interact_params,
            text_message="Hello, I need help with my account",
        ):
            print(event)

        # 3. Finish
        await client.conversations.finish_conversation(conversation_id)

Key considerations:

  • Always call finish_conversation when done to release server resources

  • Handle the NDJSON stream event-by-event -- don't buffer the entire response

  • Use abort_event (Python) or AbortController (TypeScript) for cancellation


2. User Model Enrichment Pipeline

Create users, attach context, and query their enriched user model.

spinner

3. Webhook-Driven Memory Sync

Receive conversation events via webhooks and sync memories to an external system.

spinner

Key considerations:

  • Always use constant-time comparison for signature verification

  • Respond with 200 quickly -- process events asynchronously if needed

  • Implement idempotency using the event ID to handle retries


4. Multi-Service Routing

List available services and route conversations to the appropriate one based on user needs.

spinner

5. Simulation-Driven Deployment

Run automated simulations before deploying agent changes to production.

spinner

Key considerations:

  • Use --test-user for parallel simulation execution without conflicts

  • Set --analyze-wait to allow time for metrics computation after simulation

  • Define clear pass/fail thresholds for your metrics (e.g., safety >= 9.0)

Last updated

Was this helpful?