Skip to content

Better Error Handling For Long Contexts #1093

Open
@Filimoa

Description

@Filimoa

Please read this first

  • Have you read the docs?Agents SDK docs
    Yes
  • Have you searched for related issues? Others may have had similar requests
    Yes

Describe the feature

This may be out of scope since it varies from model to model, but it would be incredibly useful to have some sort of abstraction over handling long context windows.

Currently we have some RAG tools that can overflow the context windows.

{
  "error": "Error code: 400 - {'error': {'message': 'Your input exceeds the context window of this model. Please adjust your input and try again.', 'type': 'invalid_request_error', 'param': 'input', 'code': 'context_length_exceeded'}}"
}

Our current workaround is wrapping the outputs of tools with truncate_str_to_n_tokens. This is quite cumbersome to do on every single possible tool.

@function_tool(name_override="read_document")
async def read_document(
    ctx: RunContextWrapper[SearchAgentContext],
    document_ids: list[str],
) -> str:
    """
    Fetch the details of a specific attachment by its document id. This includes the first 50 pages of the document.
    """
    async with AsyncSessionLocal() as db:
        docs = await get_full_documents(db, document_ids)
    return ai.truncate_str_to_n_tokens(
        format_as_xml(docs),
        100_000, 
    )

Maybe this is already a solved problem, but it would be helpful to have more documentation on best practices.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions