Skip to main content

The Context Object

The context parameter provides access to conversation history and user data in your agent functions.
@cycls.app()
async def app(context):
    # Access conversation history
    messages = context.messages

    # Access the latest message
    latest_msg = messages[-1]

    yield f"You said: {latest_msg['content']}"

Context Properties

context
Context Object

Message Format

The context.messages follows the standard OpenAI message format, which can include text and images (multimodal). Here is an example of what context.messages looks like:
[
    {'role': 'user', 'content': 'hello'},
    {'role': 'assistant', 'content': 'Hello! How can I assist you today?'},
    {'role': 'user', 'content': [
        {'type': 'image_url', 'image_url': {'url': 'https://.../image.png'}}
    ]},
    {'role': 'assistant', 'content': "It looks like you're looking at an image..."},
]

Working with Messages

You can easily process these messages to extract text or pass them directly to an LLM.
import cycls

@cycls.app(pip=["openai"])
async def app(context):
    from openai import AsyncOpenAI

    client = AsyncOpenAI()
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=context.messages,
        stream=True
    )
    async for chunk in response:
        content = chunk.choices[0].delta.content
        if content:
            yield content

app.local()

Next Steps

Customize UI

Tailor the chat interface to match your brand.