Best practices

This page includes recommendations on how to work with the Dialogflow API. These guidelines are designed for greater efficiency and accuracy as well as reasonable response times from the API.

Agent design

Your agent design can greatly impact the quality and performance of your agent. See the Agent design guide for best practices.

Session client scope

You can improve the performance of your code by creating a session client that scopes to multiple requests.

Requests to the API must include a token that grants access to the API and identifies your Google Cloud Platform project and account. When you create a session client object using the client libraries, the client libraries obtain an access token based on credentials that you provide, or your application default credentials. The additional processing to obtain the access token can slow down your request to the API by one or two seconds.

To improve performance, you can use a single instance of a session client object for multiple requests. The session client reuses the same access token for as long as it is valid (typically one hour). Once it expires, the session client refreshes the access token automatically, so you don't need to recreate the session client to refresh your access token. Your request that also refreshes the access token can take an extra second or two.

While this approach will perform better, your code will be less portable in that you cannot include the code to create the session client in a single method call, that could be easily copied to another project.

For example, the following Node.js client library code will perform better as the same session client is used for multiple calls to the detectTextIntent method:

// Import the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiate a session client
const sessionClient = new dialogflow.SessionsClient();

function detectTextIntent(projectId, sessionId, queries, languageCode) {
  if (!queries || !queries.length) {
    return;
  }

  // The path to identify the agent that owns the created intent.
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  ...

Alternatively, the following Node.js client library code would be considered more portable code as it includes the creation of the session client in the detectTextIntent method:

function detectTextIntent(projectId, sessionId, queries, languageCode) {
  // Import the Dialogflow library
  const dialogflow = require('dialogflow');

  // Instantiate a session client
  const sessionClient = new dialogflow.SessionsClient();

  if (!queries || !queries.length) {
    return;
  }

  // The path to identify the agent that owns the created intent.
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  ...

For more information on accessing the API using the client libraries, see Client libraries.

Was this page helpful? Let us know how we did:

Send feedback about...

Dialogflow Enterprise Edition Documentation