OCI GenAI Agent Runtime throws 404 'Unknown resource' error when called from Oracle Function

I’m building an Oracle Cloud application and need to call a Generative AI Agent from within an Oracle Function using Python SDK. The plan is to integrate this with Oracle APEX so the agent can execute SQL queries against our database.

When I try to invoke the agent, I keep getting a 404 error saying ‘Unknown resource’. Here’s what the error looks like:

{
  "status": "success",
  "ai_response": "Error calling AI agent: {
    'target_service': 'generative_ai_agent_runtime',
    'status': 404,
    'code': '404',
    'opc-request-id': '3A74B8E9C25341A7B2F1E8D6A9E47CB5/17A6F93E8B72947D84C5B6A735E2F8AC/8E1D4A67F3C88B5A9F7E3D2A6B9F8E4C',
    'message': 'Unknown resource 5fa33dc8-ac67-4fe9-a287-ed834bb513bb',
    'operation_name': 'chat',
    'timestamp': '2025-06-11T10:22:18.456789+00:00',
    'client_version': 'Oracle-PythonSDK/2.154.1',
    'request_endpoint': 'POST https://agent-runtime.generativeai.eu-frankfurt-1.oci.oraclecloud.com/20240531/agentEndpoints/ocid1.genaiagentendpoint.oc1.eu-frankfurt-1.amaaaaaabumsjqaa7h5ycnwvza9prjyxdslazguxym8hglcn4fvpqjg3amrx/actions/chat'
  }"
}

My Python function code:

import io
import os
import json
import oci
import uuid
import logging
from oci.config import from_file

def invoke_ai_agent(user_input):
    try:
        # Initialize with Resource Principal authentication
        auth_signer = oci.auth.signers.get_resource_principals_signer()

        ai_agent_client = oci.generative_ai_agent_runtime.GenerativeAiAgentRuntimeClient(
            config={}, signer=auth_signer
        )

        conversation_id = str(uuid.uuid4())

        agent_response = ai_agent_client.chat(
            agent_endpoint_id="ocid1.genaiagentendpoint.oc1.eu-frankfurt-1.amaaaaaabumsjqaa7h5ycnwvza9prjyxdslazguxym8hglcn4fvpqjg3amrx",
            chat_details=oci.generative_ai_agent_runtime.models.ChatDetails(
                user_message=user_input,
                session_id=conversation_id
            )
        )

        return agent_response.data

    except Exception as error:
        return f"Error calling AI agent: {str(error)}"

I’ve already verified that the agent and endpoint exist in eu-frankfurt-1 region. The Dynamic Group is set up properly with IAM policies allowing access to generative-ai-family resources. Authentication works fine since I’m not getting auth errors. The OCIDs are copied directly from the console and the agent shows as active.

Any ideas why this 404 error keeps happening?

I’ve debugged similar issues and it’s usually the agent endpoint config, not the OCID.

First - check your agent endpoint is configured for Oracle Functions. In OCI console, go to agent endpoint settings and make sure “Allowed Services” includes Oracle Functions.

Second common issue is session handling. Don’t generate random UUIDs for conversation_id. Use a deterministic session ID or check if your agent needs session initialization first.

Also check your agent’s knowledge base status. Agent might show active but the knowledge base could still be indexing or failed during setup.

One more thing - resource principal auth is right, but your function’s dynamic group policy needs to specifically include the generative AI agent runtime service, not just general generative-ai-family.

Add debug logging to check endpoint status before the chat call:

# Add this before the chat call
endpoint_response = ai_agent_client.get_agent_endpoint(
    agent_endpoint_id="your-endpoint-id"
)
logging.info(f"Endpoint status: {endpoint_response.data.lifecycle_state}")

This shows you if the endpoint’s actually ready for requests.

I ran into this too! the endpoint sometimes needs extra time to be ready, even when it shows as active. try recreating the endpoint or double-check your region settings. make sure everything’s configured correctly!