The code sample demonstrates how to use the Vertex AI Generative Models API to count the number of tokens in a prompt and generate content using the Gemini model.

## Code sample

### Go


Before trying this sample, follow the Go setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Go API
reference documentation](https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    import (
    	"context"
    	"fmt"
    	"io"
    	"mime"
    	"path/filepath"

    	"cloud.google.com/go/vertexai/genai"
    )

    // countTokensMultimodal finds the number of tokens for a multimodal prompt (video+text), and writes to w. Then,
    // it calls the model with the multimodal prompt and writes token counts from the response metadata to w.
    //
    // video is a Google Cloud Storage path starting with "gs://"
    func countTokensMultimodal(w io.Writer, projectID, location, modelName string) error {
    	// location := "us-central1"
    	// modelName := "gemini-2.0-flash-001"
    	prompt := "Provide a description of the video."
    	video := "gs://cloud-samples-data/generative-ai/video/pixel8.mp4"

    	ctx := context.Background()

    	client, err := genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_NewClient(ctx, projectID, location)
    	if err != nil {
    		return fmt.Errorf("unable to create client: %w", err)
    	}
    	defer client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_Close()

    	model := client.GenerativeModel(modelName)

    	part1 := genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Text(prompt)

    	// Given a video file URL, prepare video file as genai.Part
    	part2 := genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FileData{
    		MIMEType: mime.TypeByExtension(filepath.Ext(video)),
    		FileURI:  video,
    	}

    	// Finds the total number of tokens for the 2 parts (text, video) of the multimodal prompt,
    	// before actually calling the model for inference.
    	resp, err := model.CountTokens(ctx, part1, part2)
    	if err != nil {
    		return err
    	}

    	fmt.Fprintf(w, "Number of tokens for the multimodal video prompt: %d\n", resp.TotalTokens)

    	res, err := model.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_GenerateContent(ctx, part1, part2)
    	if err != nil {
    		return fmt.Errorf("unable to generate contents: %w", err)
    	}

    	// The token counts are also provided in the model response metadata, after inference.
    	fmt.Fprintln(w, "\nModel response")
    	md := res.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_UsageMetadata
    	fmt.Fprintf(w, "Prompt Token Count: %d\n", md.PromptTokenCount)
    	fmt.Fprintf(w, "Candidates Token Count: %d\n", md.CandidatesTokenCount)
    	fmt.Fprintf(w, "Total Token Count: %d\n", md.TotalTokenCount)

    	return nil
    }

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=generativeaionvertexai).