function recordChatCompletionEvents({
  segment,
  transaction,
  messages,
  events,
  metadata,
  tags,
  err,
  shim
}) {
  const { pkgVersion, agent } = shim
  segment.end()

  if (shouldSkipInstrumentation(shim.agent.config) === true) {
    // We need this check inside the wrapper because it is possible for monitoring
    // to be disabled at the account level. In such a case, the value is set
    // after the instrumentation has been initialized.
    shim.logger.debug('skipping sending of ai data')
    return
  }

  const completionSummary = new LangChainCompletionSummary({
    agent,
    messages,
    metadata,
    tags,
    segment,
    transaction,
    error: err != null,
    runId: segment[langchainRunId]
  })

  common.recordEvent({
    agent,
    shim,
    type: 'LlmChatCompletionSummary',
    pkgVersion,
    msg: completionSummary
  })

  // output can be BaseMessage with a content property https://js.langchain.com/docs/modules/model_io/concepts#messages
  // or an output parser https://js.langchain.com/docs/modules/model_io/concepts#output-parsers
  recordCompletions({
    events,
    completionSummary,
    agent,
    segment,
    shim,
    transaction
  })

  if (err) {
    agent.errors.add(
      transaction,
      err,
      new LlmErrorMessage({
        response: {},
        cause: err,
        summary: completionSummary
      })
    )
  }

  transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_EVENT, 'llm', true)
}
Ends active segment, creates LlmChatCompletionSummary, and LlmChatCompletionMessage(s), and handles errors if they exists

@param {object} params function params
@param {TraceSegment} params.segment active segment
@param {Array} params.messages response messages
@param {Array} params.events prompt and response messages
@param {object} params.metadata metadata for the call
@param {Array} params.tags tags for the call
@param {Error} params.err error object from call
@param {Shim} params.shim shim instance
@param {Transaction} params.transaction active transaction