microsoft/semantic-kernel · error · AgentInvokeException

Failed to fetch token from token endpoint.

Error message

Failed to fetch token from token endpoint.

What it means

An AgentInvokeException raised in the no-bot_secret branch when GET token_endpoint returns a non-200 status. The endpoint rejected the request outright; the status is logged.

Source

Thrown at python/samples/demos/copilot_studio_agent/src/direct_line_agent.py:78

                        data = await resp.json()
                        self.directline_token = data.get("token")
                        if not self.directline_token:
                            logger.error("Token generation response missing token: %s", data)
                            raise AgentInvokeException("No token received from token generation.")
                    else:
                        logger.error("Token generation endpoint error status: %s", resp.status)
                        raise AgentInvokeException("Failed to generate token using bot_secret.")
            else:
                async with self.session.get(self.token_endpoint) as resp:
                    if resp.status == 200:
                        data = await resp.json()
                        self.directline_token = data.get("token")
                        if not self.directline_token:
                            logger.error("Token endpoint returned no token: %s", data)
                            raise AgentInvokeException("No token received.")
                    else:
                        logger.error("Token endpoint error status: %s", resp.status)
                        raise AgentInvokeException("Failed to fetch token from token endpoint.")
        except Exception as ex:
            logger.exception("Exception fetching token: %s", ex)
            raise AgentInvokeException("Exception occurred while fetching token.") from ex

    @trace_agent_get_response
    @override
    async def get_response(
        self,
        history: ChatHistory,
        arguments: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> ChatMessageContent:
        """
        Get a response from the DirectLine Bot.
        """
        responses = []
        async for response in self.invoke(history, arguments, **kwargs):
            responses.append(response)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Correct the TOKEN_ENDPOINT value to the real token-issuing URL.
  2. Use the bot_secret flow if the token_endpoint requires authentication.
  3. Retry on transient 5xx; treat 4xx as configuration errors.

Example fix

// before
TOKEN_ENDPOINT=https://example.com/api

// after
TOKEN_ENDPOINT=https://directline.botframework.com/v3/directline/tokens/generate
Defensive patterns

Strategy: retry

Try / catch

try:
    await fetch_token()
except AgentInvokeException as e:
    if "Failed to fetch token from token endpoint" in str(e):
        # 5xx is transient; 4xx is config
        logger.warning("Token endpoint unreachable, will retry once")
        await asyncio.sleep(2); await fetch_token()
    raise

Prevention

When it happens

Trigger: GET self.token_endpoint returns 4xx/5xx (404 wrong URL, 401 needs auth, 5xx server fault).

Common situations: token_endpoint misconfigured or stale; the token service requires auth that wasn't supplied; service outage.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/71701935a7de5d13. Report an issue: GitHub.