Best Practices for Implementing Context-Aware AI Agents with Business-Specific Context Injection

Hi AgentOS community,

I’m working on implementing context-aware AI agents using AgentOS and would like to get feedback on the best architectural approaches for injecting business context into agents at runtime.

Current Setup

  • FastAPI application integrated with AgentOS

  • Multi-tenant SaaS architecture with request-scoped business context

  • Middleware that extracts business context from request headers (user_id, customer_id, business_unit_id, etc.)

  • Context stored in request.state.context as a structured object

Goal

I want my AI agents to have access to the current request’s business context so they can make context-aware decisions and API calls. For example:

  • Filter data based on the current user’s permissions/scope

  • Make API calls with the appropriate tenant/customer context

  • Provide responses relevant to the user’s specific business unit

  • Maintain proper data isolation in multi-tenant scenarios

Current Approach - Input Schema Method

I’m currently experimenting with using a custom input schema to inject context:

class BusinessContextInput(BaseModel):
    message: str = Field(..., description="User's message")
    context: Optional[Dict[str, Any]] = Field(None, description="Business context")

agent = Agent(
    name="ContextAwareAgent",
    model=model,
    tools=tools,
    instructions=instructions,
    input_schema=BusinessContextInput
)

Then in my FastAPI endpoint, I would populate the context field with the business context extracted by middleware.

Questions

  1. Is the input schema approach the recommended way to inject runtime context into agents?

    • Are there performance implications of passing context through the input schema for every request?

    • Does this approach work well with AgentOS’s conversation memory and session management?

  2. Alternative Approaches:

    • Should I be using agent-level context injection instead?

    • Is there a way to set context on the agent instance dynamically before each conversation?

    • Would custom tools that have access to request context be a better approach?

  3. Agent Instructions and Context:

    • How should I modify agent instructions to make use of the injected context?

    • Should context be part of the system instructions or passed as user message metadata?

  4. Session Management:

    • How does business context interact with AgentOS’s session management?

    • Should each user/tenant have separate agent instances or sessions?

    • How do I ensure context doesn’t leak between different users’ sessions?

  5. Tools and Context:

    • What’s the best way to make business context available to custom tools?

    • Should tools receive context as parameters, or access it through some other mechanism?

Hi @coderfalse_us, thanks for reaching out and supporting Agno. I’ve shared this with the team, we’re working through all requests one by one and will get back to you soon. We appreciate your patience!

Hey @coderfalse_us - Thanks for the detailed questions and apologies for the delay!
Let me try and answer them for you.

  1. Using input schema isn’t the correct approach here, as this is runtime context you need to ensure is part of the session state.

  2. Using middle ware here is the right approach. Since AgentOS is just a FastAPI app, we can leverage middleware to ensure we can read claims right from the JWT and store it in AgentOS’s context.
    ```python

from fastapi import FastAPI

from agno.os import AgentOS

from agno.os.middleware.jwt import JWTMiddleware, TokenSource

app = FastAPI()
# Add JWT middleware BEFORE mounting AgentOS

app.add_middleware(

JWTMiddleware,

secret_key=“your-secret-key”,

algorithm=“HS256”,

token_source=TokenSource.HEADER,

user_id_claim=“sub”, # JWT claim name for user ID

session_id_claim=“session_id”, # JWT claim name for session ID

# these will be extracted: request.state.session_state = {“customer_id”: “123”, “tenant_id”: “abc”}

session_state_claims=[“customer_id”, “business_unit_id”, “tenant_id”],

# this will be extracted: request.state.dependencies = {“permissions”: [“read”, “write”]}

dependencies_claims=[“permissions”, “scopes”],

)

# Mount AgentOS (it will automatically read from request.state)

agent_os = AgentOS(agents=[your_agent])

app.mount(“/api”, agent_os.api)

```

  1. What we can do now is use context aware tools in order to allow the agent to pull context when it needs it.

```python

from agno.agent import Agent

from agno.models.openai import OpenAIChat

from agno.run import RunContext

def fetch_customer_data(run_context: RunContext, data_type: str) → str:

"""Tool that uses business context."""

# Access business context

customer_id = run_context.session_state\["customer_id"\]

tenant_id = run_context.session_state\["tenant_id"\]

# Access services

permissions = run_context.dependencies.get("permissions", \[\])

# Make tenant-scoped API call

return f"Data for customer {customer_id} in tenant {tenant_id}"

agent = Agent(

name=“CustomerAgent”,

model=OpenAIChat(id=“gpt-4”),

tools=[fetch_customer_data],

instructions=[

“You are a customer support agent.”,

“Current customer: {customer_id}”, # Auto-resolved from session_state

“Tenant: {tenant_id}”,

],

)

```

As seen above, you can use templating to inject context values into the agent’s instructions

  1. Session Management
    You don’t have to create multiple agent instances for multiple users. A single instance serves all your users. Sessions are scoped to users by their user_ids.

I hope this helps! Apologies for the poor formatting - copying snippets out of my IDE isn’t working so well this morning.

Let me know if there’s anything else you need!

Ruan